문제

If I run the following:

var fn = function(){}

(function(){
    fn();
}());

It throws an "TypeError: undefined is not a function" in Chrome, and "TypeError: fn is not a function" in Firebug.

Shouldn't fn be available in this scope chain, without having to do window.fn?

The bizarre part is that if I run:

var fn = function(){}
console.log(fn);

(function(){
fn();
}());

Then it behaves as expected: fn is logged as a function, and no error is thrown in either Chrome or Firefox.

This is making me feel silly, like I shouldn't ever claim to understand JS.

도움이 되었습니까?

해결책

You forgot to add semicolon:

var fn = function(){};

(function(){
    fn();
}());

다른 팁

Damn, it was ASI.

var fn = function(){};

(function(){
fn();
}());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top