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

156
Views
La variable no se volverá global

Tengo una variable en una función y quiero llamarla fuera de la función, pero dice 'Nombre no definido'. Por favor, indique cómo puedo hacer que esta variable sea global.

 n = Math.ceil(Math.random() * 80); console.log(n) function randomStarwars(){ fetch('https://swapi.dev/api/people/'+n) .then(response => response.json()) .then(data=> { var Name = data.name }) } addEventListener("click", randomStarwars) console.log(Name)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Es posible que desee echar un vistazo al alcance variable .

La razón por la que no puede acceder a él es porque está definido dentro de la función lambda .then() y solo se puede acceder allí.

Para tenerlo disponible fuera de esa función, debe declararlo fuera de ella:

 n = Math.ceil(Math.random() * 80); var Name = ""; // Here, for example console.log(n) function randomStarwars(){ fetch('https://swapi.dev/api/people/'+n) .then(response => response.json()) .then(data=> { Name = data.name; // Remove 'var', because it's already declared }) } addEventListener("click", randomStarwars) console.log(Name)

Actualización después de tu comentario:

No ve el valor en su console.log porque el valor se asigna dentro de una Promesa , que nunca se ejecuta correctamente cuando se declara.

Para console.log el Name de manera efectiva, debe encadenar otro .then() y registrarlo allí:

 n = Math.ceil(Math.random() * 80); var Name = ""; // Here, for example console.log(n) function randomStarwars(){ fetch('https://swapi.dev/api/people/'+n) .then(response => response.json()) .then(data=> { Name = data.name; // Remove 'var', because it's already declared }) .then(() => { console.log(Name) // This guarantees the value has been set, aside from any errors that might occur before }) } addEventListener("click", randomStarwars) console.log(Name) // This is executed before the Name is set
about 4 years ago · Juan Pablo Isaza Report

0

debe declarar Variable antes de la función de búsqueda

 var Name = ""; n = Math.ceil(Math.random() * 80); console.log(n) function randomStarwars(){ fetch('https://swapi.dev/api/people/'+n) .then(response => response.json()) .then(data=> { Name = data.name }) } addEventListener("click", randomStarwars) console.log(Name)
about 4 years ago · Juan Pablo Isaza Report

0

Defina su variable fuera de sus funciones:

 var global_Name; // this is your global variable n = Math.ceil(Math.random() * 80); console.log(n) function randomStarwars(){ fetch('https://swapi.dev/api/people/'+n) .then(response => response.json()) .then(data=> { global_Name= data.name // use it without var }) } addEventListener("click", randomStarwars) console.log(Name) function2() { global_Name = // use it without var } function3() { global_Name = // use it without var }
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!