2 ms·
var x = (function(){ var inner_foo = 1; //internal to closure return function(){ var foo = 'foo'; var bar = foo + inner_foo; //this function has acc
by geuis 16y ago
var x = (function(){
var inner_foo = 1; //internal to closure
return function(){
var foo = 'foo';
var bar = foo + inner_foo;
//this function has access to variables from the outer scope.
//inner_foo is still accessible
console.log( inner_foo, foo, bar ); //1, 'foo', 'foo1'
}
})();
//Edit a couple minutes later:
I'm realizing I may not be understanding exactly what you're asking for in the 2nd question. If you elaborate a bit...
Since an event will execute either an assigned or anonymous function, the same rules towards variable scope in your closures still applies when the events execute. Having a function executed by a click event isn't fundamentally different than executing a function directly.
- tantalor 16y agoIt is when it's a function of an object.