函数声明和函数表达式的理解

  JScript和ECMAScript的变现不一致,但是对于现在的w3c规范来说都是遵循ECMAScript的。所以函数申明和函数表达式对JScript的表现在此就不做过多的介绍,可以移步汤姆大叔blog深入学习:揭秘命名函数表达式

1 function foo(){ return 1; }//函数声明
2 var foo = function(){ return 1; };//函数表达式
 1 function a(){
 2   alert(foo());//1
 3   function foo(){ return 1; }//函数声明(提升到作用域的最顶端)
 4 
 5 }
 6 function b(){
 7   alert(foo());//undefined is not a function
 8    var foo = function(){ return 1; };//函数表达式(只是把变量名提升到了作用域的最顶端,函数实现并未提升到最顶端)
 9 }
10 a();//1
11 b();//undefined is not a function
对于这个形式的解析:
1 var foo = function bar(){ return 1; };//函数表达式
2 alert(bar())//bar is not defined
3 alert(foo())//1
在JScript中会出现以下表现
1 var f = function g(){
2     return [
3       arguments.callee == f,
4       arguments.callee == g
5     ];
6   };
7 f(); // [true, false]
8 g(); // [false, true]
在ECMAScript中的表现是:g is undefined;  
在ECMAScript中函数表达式声明函数的时候对于后面的表达式默认是匿名函数,如果起了名字任然不起作用(PS:这是我的理解)
 
原文地址:https://www.cnblogs.com/cdwp8/p/4059728.html