今天看到代码
function fn1() {
console.log('fn1')
}
var fn2
fn1()
fn2()
fn2 = function() {
console.log('fn2')
}
fn2()
// 会报错 因为fn2并没有提升
输出结果:
给出的理由很简单,说
是因为第一个fn2(),这时fn2仍是一个undefined的变量。
其实并不全面。
function fn1() {
console.log('fn1')
}
var fn2
fn1()
fn2()
function fn2(){
console.log('fn2')
}
fn2()
把代码改成这样,输出结果
成功运行了,这里要补充的是,函数声明是可以提升的,
function functionName(){ xxx} 这种就是函数声明
而什么是函数表达式 xx = function(){ xxxxx} 这就是函数表达式