Discuss
/
JavaScript
/
函数声明和函数表达式不完全一样。
函数声明和函数表达式不完全一样。
Topic sourcefunction a(){ //.... }
var b = function () { //.... }
这两种函数的声明是有区别的,第一种会先加载,不管函数调用的先后,而第二种,只有在给b赋值的时候才去加载那个函数,然后赋值给b变量
a(); b();
function a(){ //.... }
var b = function () {
//....
}
我这边报错的是:
a();
VM21590:1 Uncaught ReferenceError: a is not defined
at <anonymous>:1:1
(anonymous) @ VM21590:1
b()
VM21596:1 Uncaught ReferenceError: b is not defined
at <anonymous>:1:1
(anonymous) @ VM21596:1
function a(){
//....
}
undefined
var b = function () {
//....
}
undefined
- 1
龙雀
变量提升时,函数声明的变量名字和值都会提升,而函数表达式只提升名字。
例如:
a()
能正常调用,而b()
会报错。