Let's explore a straightforward scenario:
(function x(){
var foo = function(){
console.log('foo is alive!');
// set 'foo' variable to an empty function, using a delay
setTimeout(function(){
foo = function(){};
},0);
}
foo();
// calling 'foo' again, after it should be an empty function
setTimeout(foo,100);
})();
When you execute this code in your console, it will print foo is alive!
twice. I am puzzled about why the foo
variable isn't replaced with the empty function. The timeout callback recognizes foo
as a variable pointing to the function.
Dull backstory for this inquiry:
This test case is a simplified version of a more intricate situation involving AJAX callbacks instead of the timeout present in this example.