Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
No se puede usar un método dentro de una clase

Estoy tratando de hacer una clase de matriz. La función cof() no funciona cuando la uso como método y muestra este error.
práctica: 47 TypeError no capturado: no se puede leer la propiedad 'cof' de undefined.

Pero cuando lo uso dentro del método det() como función, funciona perfectamente. ¿Puede alguien explicar por qué ocurre esto?

 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()
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Cuando se ejecuta la función recursiveDeterminantMatrix , this se evalúa como la instancia de la clase, porque no es un método, solo una función simple.

Puede guardar la instancia de la clase en una variable self para su uso adecuado en la función interna.

 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); }

Para obtener más información sobre this palabra clave, consulte los documentos de MDN .

about 4 years ago · Juan Pablo Isaza Report

0

Puedes usar bind para esto.

 recursiveDeterminantMatrix(this.cof(matrix, 0, i).bind(this));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!