Pluralsight Closure Demo
by toby3105
JavaScript
function myNonClosure() {
var date = new Date();
return date.getMilliseconds();
}
function myClosure() {
var date = new Date();
return function () {
return date.getMilliseconds();
};
}
var myClosure2 = function () {
var date = new Date();
var myNestedFunction = function () {
return date.getMilliseconds();
};
return {
foo: myNestedFunction
};
}
var closure = new myClosure2();
console.log(closure.foo());
window.setTimeout(function () {
console.log(closure.foo());
}, 500);