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

146
Views
Ejemplo IIFE de MDN

Estoy viendo el siguiente ejemplo de MDN sobre casos de uso para IIFE. No entiendo por qué es necesario el IIFE. Incluso si reescribo makeWithdraw como solo una declaración de función tomando copyBalance como parámetro, el resultado es el mismo. Entiendo que esto se debe al alcance funcional y que el uso de IIFE podría tener otros casos de uso sobre espacios de nombres globales y cierres. Pero, ¿qué está tratando de mostrarnos MDN aquí? De alguna manera no he aprendido correctamente algo. ¿Alguien puede ayudarme a volver a la normalidad?

 const makeWithdraw = balance => (function(copyBalance) { let balance = copyBalance; // This variable is private let doBadThings = function() { console.log("I will do bad things with your money"); }; doBadThings(); return { withdraw: function(amount) { if (balance >= amount) { balance -= amount; return balance; } else { return "Insufficient money"; } }, } })(balance); const firstAccount = makeWithdraw(100); // "I will do bad things with your money" console.log(firstAccount.balance); // undefined console.log(firstAccount.withdraw(20)); // 80 console.log(firstAccount.withdraw(30)); // 50 console.log(firstAccount.doBadThings); // undefined, this method is private const secondAccount = makeWithdraw(20); // "I will do bad things with your money" secondAccount.withdraw(30); // "Insufficient money" secondAccount.withdraw(20); // 0

Tengo que estudiar más sus respuestas, pero en respuesta al comentario sobre reproducibilidad, mis disculpas, esto es lo que quise decir con declaración de función:

 function makeWithdraw (balance) { let doBadThings = function() { console.log("I will do bad things with your money"); }; doBadThings(); return { withdraw: function(amount) { if (balance >= amount) { balance -= amount; return balance; } else { return "Insufficient money"; } }, } }; const firstAccount = makeWithdraw(100); // "I will do bad things with your money" console.log(firstAccount.balance); // undefined console.log(firstAccount.withdraw(20)); // 80 console.log(firstAccount.withdraw(30)); // 50 console.log(firstAccount.doBadThings); // undefined, this method is private const secondAccount = makeWithdraw(20); // "I will do bad things with your money" console.log(secondAccount.withdraw(30)); // "Insufficient money" secondAccount.withdraw(20); // 0
about 4 years ago · Juan Pablo Isaza
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!