Ejemplo 1: cierre en js
A closure gives you access to an outer function’s scope from an inner function
//example
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
alert(name); // use variable declared in the parent function
}
displayName();
}
init();
Ejemplo 2: cierres en javascript
function OuterFunction() {
var outerVariable = 100;
function InnerFunction() {
alert(outerVariable);
}
return InnerFunction;
}
var innerFunc = OuterFunction();
innerFunc(); // 100
Ejemplo 3: cierre en javascript
function makeFunc() {
var name = 'Mozilla';
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)