84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
var add1 = function(a,b){ return a+b; } function add2(a,b){ return a+b; }
如图,上面两个函数的声明方式有什么区别吗?
用函数语句创建的函数add2,函数名称和函数体均被提前,在声明它之前就使用它。但是使用var表达式定义函数add1,只有变量声明提前了,变量初始化代码仍然在原来的位置,没法提前执行。运行结果如下:
console.log(add2(1,1)); //输出2 function add2(a,b){ return a+b; } console.log(add1(1,1)); //报错:add1 is not a function var add1 = function(a,b){ return a+b; }
说一个不容易注意的区别吧递归的时候
function bar(){ ...... }
在函数体内可以用bar调用到自己
var foo = function(a,b){ ...... }
foo则不行
声明函数具有函数提升效果,可以在声明函数的代码前执行函数
用函数语句创建的函数add2,函数名称和函数体均被提前,在声明它之前就使用它。
但是使用var表达式定义函数add1,只有变量声明提前了,变量初始化代码仍然在原来的位置,没法提前执行。
运行结果如下:
说一个不容易注意的区别吧
递归的时候
在函数体内可以用bar调用到自己
foo则不行
声明函数具有函数提升效果,可以在声明函数的代码前执行函数