Closure
내부함수
Example
function celebrityIDCreator(theCelebrities) { var i; var uniqueID = 100; for (i = 0; i < theCelebrities.length; i++) { theCelebrities[i]["id"] = function() { return uniqueID + i; } } return theCelebrities; } var actionCelebs = [{name:"Stallone", id:0}, {name:"Cruise", id:0}, {name:"Willis", id:0}]; var createIdForActionCelebs = celebrityIDCreator(actionCelebs); var stalloneID = createIdForActionCelebs[0]; console.log(stalloneID.id()); var cruiseID = createIdForActionCelebs[1]; console.log(cruiseID.id()); var willisID = createIdForActionCelebs[2]; console.log(willisID.id());
103 103 103
function celebrityIDCreator(theCelebrities) { var i; var uniqueID = 100; for (i = 0; i < theCelebrities.length; i++) { theCelebrities[i]["id"] = function(j) { return function() { return uniqueID + j; }() }(i); } return theCelebrities; } var actionCelebs = [{name:"Stallone", id:0}, {name:"Cruise", id:0}, {name:"Willis", id:0}]; var createIdForActionCelebs = celebrityIDCreator(actionCelebs); var stalloneID = createIdForActionCelebs[0]; console.log(stalloneID.id); var cruiseID = createIdForActionCelebs[1]; console.log(cruiseID.id); var willisID = createIdForActionCelebs[2]; console.log(willisID.id);
100 101 102