예)
function genClosure(i) {
return function () {
i++;
return i;
}
}
var closure1 = genClosure(0);
console.log(closure1()); // 1
console.log(closure1()); // 2
console.log(closure1()); // 3
var closure2 = genClosure(100);
console.log(closure2()); // 101
console.log(closure2()); // 102
console.log(closure2()); // 103
console.log(closure1()); // 4
var i = 10;
var closure3 = genClosure(i);
console.log(closure3()); // 11
console.log(closure3()); // 12
console.log(i); // 10
댓글 없음:
댓글 쓰기