无法在 class 中使用方法
Not able to use a method inside a class
我正在尝试制作一个矩阵 class。 cof() 函数在我将其用作方法并显示此错误时不起作用。
实践:47 未捕获类型错误:无法读取未定义的 属性 'cof'。
但是当我在 det() 方法中将它作为函数使用时,它运行良好。谁能解释为什么会这样?
class Matrix{
constructor(matrix){
this.matrix = matrix
}
cof(matrix = this.matrix, i, j) {
let cofMat = matrix.map(row =>
row.filter((_, colIndex) => j !== colIndex)
)
cofMat.splice(cofMat[i],1)
return cofMat
}
det(matrix = this.matrix) {
let validInput = true;
//input validation
let columnLength = matrix.length;
matrix.forEach((row) =>
row.length === columnLength ? (validInput = true) : (validInput = false)
);
if (!validInput) return "Input a valid n*n matrix";
// determining the matrix using 1st row.
// This function is not working properly as a method
function cof(matrix, i, j) {
let cofMat = matrix.map(row =>
row.filter((_, colIndex) => j !== colIndex)
)
cofMat.splice(cofMat[i],1)
return cofMat
}
function recursiveDeterminantMatrix(matrix) {
if (matrix.length === 2 && matrix[0].length === 2) {
let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return result;
} else {
let answer = 0;
for(let i =0; i< matrix.length; i++) {
let cofactor =
(-1) ** i *
matrix[0][i] *
recursiveDeterminantMatrix(this.cof(matrix, 0, i));
answer += cofactor;
};
return answer;
}
}
return recursiveDeterminantMatrix(matrix);
}
}
let matrix = [[1,2,3],[4,5,6],[7,8,8]];
let mat = new Matrix(matrix).det()
当 recursiveDeterminantMatrix
函数执行时,this
不会对 class 的实例求值,因为它不是方法,只是一个普通函数。
您可以将 class 的实例保存在 self
变量中,以便在内部函数中正确使用。
det(matrix = this.matrix) {
const self = this;
// ...
function recursiveDeterminantMatrix(matrix) {
if (matrix.length === 2 && matrix[0].length === 2) {
let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return result;
} else {
let answer = 0;
for(let i =0; i< matrix.length; i++) {
let cofactor =
(-1) ** i *
matrix[0][i] *
recursiveDeterminantMatrix(self.cof(matrix, 0, i));
answer += cofactor;
};
return answer;
}
}
return recursiveDeterminantMatrix(matrix);
}
有关this
关键字的更多信息,请查看MDN Docs
您可以为此使用绑定。
recursiveDeterminantMatrix(this.cof(matrix, 0, i).bind(this));
我正在尝试制作一个矩阵 class。 cof() 函数在我将其用作方法并显示此错误时不起作用。
实践:47 未捕获类型错误:无法读取未定义的 属性 'cof'。
但是当我在 det() 方法中将它作为函数使用时,它运行良好。谁能解释为什么会这样?
class Matrix{
constructor(matrix){
this.matrix = matrix
}
cof(matrix = this.matrix, i, j) {
let cofMat = matrix.map(row =>
row.filter((_, colIndex) => j !== colIndex)
)
cofMat.splice(cofMat[i],1)
return cofMat
}
det(matrix = this.matrix) {
let validInput = true;
//input validation
let columnLength = matrix.length;
matrix.forEach((row) =>
row.length === columnLength ? (validInput = true) : (validInput = false)
);
if (!validInput) return "Input a valid n*n matrix";
// determining the matrix using 1st row.
// This function is not working properly as a method
function cof(matrix, i, j) {
let cofMat = matrix.map(row =>
row.filter((_, colIndex) => j !== colIndex)
)
cofMat.splice(cofMat[i],1)
return cofMat
}
function recursiveDeterminantMatrix(matrix) {
if (matrix.length === 2 && matrix[0].length === 2) {
let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return result;
} else {
let answer = 0;
for(let i =0; i< matrix.length; i++) {
let cofactor =
(-1) ** i *
matrix[0][i] *
recursiveDeterminantMatrix(this.cof(matrix, 0, i));
answer += cofactor;
};
return answer;
}
}
return recursiveDeterminantMatrix(matrix);
}
}
let matrix = [[1,2,3],[4,5,6],[7,8,8]];
let mat = new Matrix(matrix).det()
当 recursiveDeterminantMatrix
函数执行时,this
不会对 class 的实例求值,因为它不是方法,只是一个普通函数。
您可以将 class 的实例保存在 self
变量中,以便在内部函数中正确使用。
det(matrix = this.matrix) {
const self = this;
// ...
function recursiveDeterminantMatrix(matrix) {
if (matrix.length === 2 && matrix[0].length === 2) {
let result = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return result;
} else {
let answer = 0;
for(let i =0; i< matrix.length; i++) {
let cofactor =
(-1) ** i *
matrix[0][i] *
recursiveDeterminantMatrix(self.cof(matrix, 0, i));
answer += cofactor;
};
return answer;
}
}
return recursiveDeterminantMatrix(matrix);
}
有关this
关键字的更多信息,请查看MDN Docs
您可以为此使用绑定。
recursiveDeterminantMatrix(this.cof(matrix, 0, i).bind(this));