When answering another question, I was using this pattern to call a function recursively:
(function() {
// ...
if(should_call_again) arguments.callee();
})();
which worked. I got feedback that naming the function also worked:
(function func() {
// ...
if(should_call_again) func();
})();
However, with this technique, window.func
is undefined
, which came as a surprise to me.
If I put it simply, my question is: why is the following true?
function a() {}
typeof window.a; // "function"
(function b() {})
typeof window.b; // "undefined"
b
is still be accessible inside b
itself. So it seems like the ( )
create another scope, but that cannot be the case because only functions create another scope, and I'm just wrapping it inside ( )
.
So why does wrapping a function inside ( )
not put the function into the global object?