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

123
Views
Estaba revisando el tema "Cierres" en JS e intenté hacer algunos experimentos con este concepto. Creé dos funciones y verifiqué su salida.

ambas funciones imprimen el valor dentro de la función "exterior" e "interior" dos veces.

usando la declaración y ejecución de funciones simples

 function outer1() { var j = 20; function inner1() { var k = 30; console.log(j, k); j++; k++; } inner1(); //calling inner1 return; } outer1(); // calling function 2 times outer1();

 output : // same output both time 20 30 20 30

-xxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx//

usando expresión de función

 function outer() { var a = 20; var inner = function() { var b = 30; console.log(a, b); a++; b++; } return inner; //returning inner } var func = outer(); func(); // calling function 2 times func();

 output : // value of a is incremented by 1 time. 20 30 21 30

Estoy confundido, ¿por qué ambos muestran resultados diferentes? ¿Por qué el resultado sigue siendo el mismo para el primero pero diferente para el segundo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Con el primer ejemplo, ejecutas outer() varias veces. Esto crea una nueva función interna cada vez, cada una con el valor inicial de j .

Si está familiarizado con las clases, esto es como llamar a new Thing().do(); new Thing().do(); vs const thing = new Thing(); thing.do(); thing.do();

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!