发现一个好博客:Exploring Javascript by example , 其中一篇文章讨论了 function declaration 以及 named function expression ,讲得很通俗,但是最后那个问题文章没有细讲.
起源:
在 jquery 源码中看到,原先的类似
var b=function(){ if(..) arguments.callee(); };
由于ecmascript-5th不推荐使用callee (strict mode 访问arguments.callee/caller发生异常),变为:
var b=function a(){ if(..) a(); };
问题:
但是由于代码的不规范以及过于复杂,有时会出现:
var b=function a(i){ if(i!=0) { alert(i); //递归一下 a(i-1); } }; //.... //... alert(a); //不小心重名了 var a='x'; alert(a); b(2); alert(a);
可以分别在 ie 和 其他标准浏览器中运行看看,其中 ie 出错了
规范:
13 function definition 中提到的:
1. Let funcEnv be the result of calling NewDeclarativeEnvironment passing the running execution context’s Lexical
Environment as the argument
2. Let envRec be funcEnv’s environment record.
3. Call the CreateImmutableBinding(N) concrete method of envRec passing the String value of Identifier as the
argument.
4. Let closure be the result of creating a new Function object as specified in 13.2 with parameters specified by
FormalParameterListopt and body specified by FunctionBody. Pass in funcEnv as the Scope. Pass in true as the
Strict flag if the FunctionExpression is contained in strict code or if its FunctionBody is strict code.
5. Call the InitializeImmutableBinding(N,V) concrete method of envRec passing the String value of Identifier and
closure as the arguments.
6. Return closure.
NOTE The Identifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to
allow the function to call itself recursively. However, unlike in a FunctionDeclaration, the Identifier in a FunctionExpression
cannot be referenced from and does not affect the scope enclosing the FunctionExpression.
我的解释:
可见:在标准中
var b=function a(i){ if(i!=0) { alert(i); //递归一下 a(i-1); } }; //相当于添加closure var b=function(){ var a; return a=function (i){ if(i!=0) { alert(i); //递归一下 a(i-1); } } }();
而在ie中相当于:
var b=function (i){ if(i!=0){ alert(i); a(i-1); } }; function a(i){ if(i!=0) { alert(i); //递归一下 a(i-1); } }; alert(b === a); //false
则出错就不意外了!