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

223
Views
No se pueden leer las propiedades de uso indefinido "Esto" en la clase de JavaScript

Estoy usando Node JS y ExpressJS para escribir mi servidor web. Uso JavaScript OOP desde hace poco tiempo. Me sale un error al ejecutar esta clase:

 class myClass { constructor(path) { this.path = path; } myFunction(){ var fileControllerInstance = new FileController(this.path); fileControllerInstance.fileExist(function(fileExist) { if(fileExist){ console.log("file exist"); this.printLine("test"); } else return false; }); } printSTR(str){ console.log(str); } } new myClass("filePath").myFunction(); module.exports = myClass;

Al ejecutar esta clase, aparece un error en la función printSTR. El error es el siguiente:

 file exist TypeError: Cannot read properties of undefined (reading 'printSTR')

Sin this obtengo ReferenceError: printSTR is not defined . Para resolver mi problema, necesito crear otra instancia de clase y usarla para llamar a la función. Algo como esto:

 new myClass("filePath").printSTR("test") instead to ``` this.printLine("test"); ```

¿Por qué usar this código no funciona? Gracias

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Dentro de la function(fileExist) , this tiene un valor diferente que fuera. Para heredar el valor interior, debe vincular la función:

 fileControllerInstance.fileExist(function(fileExist) { ... }.bind(this));
over 4 years ago · Santiago Trujillo Report

0

Estás llamando a this dentro de una devolución de llamada. Puede encontrar esta publicación útil para resolver su problema.

También intenta llamar a printLine("test") pero su método es printSTR(str)

over 4 years ago · Santiago Trujillo 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!