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

149
Views
Clase de función javascript que no encuentra la variable

Tengo dos documentos javascript en mi proyecto. js (script.js) #1:

 let func = new Function('return thisvar') console.log(func())

js (script1.js) #2:

 let thisvar = 'hello'

Recibo un error: ERROR:{@script.js line 4: cannot find variable 'thisvar'} Intenté usar var en lugar de let o incluso window.thisvar ¿Qué estoy haciendo mal?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Cuando ejecuta func() en su primer script, aún no ha definido thisvar .

Simplemente mueva la llamada de función a su segundo script después de definir la variable.

 let func = new Function('return thisvar')
 let thisvar = 'hello' console.log(func())
about 4 years ago · Santiago Gelvez Report

0

Ejemplo legible de "hola mundo" (La console.log(func(your_error) devuelve el mismo error: no se puede encontrar la variable (no declaraste la variable).

 <span>example</span> <script> let func = new Function("arg1", "return arg1") let thisvar = 'hello'; /* missing in your code */ console.log(func(thisvar)) // expected output: hello console.log(func("world")) // expected output: world console.log(func(your_error)) // expected output: "Uncaught ReferenceError: your_error is not defined" </script>

En general, es más eficiente usar expresión de función o instrucción de función.

Lea más aquí: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function

about 4 years ago · Santiago Gelvez 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!