JavaScript Hoisting(提升)

Hoisting 是指 js 在执行代码前,默认会将变量的声明和函数的声明,提升到当前作用域顶端的行为。

这里要注意一下,只提升声明,例如:

console.log(a);
var a = 10;
//输出undefined,因为只将var a;提升到了作用域顶端,而a = 10;未提升
console.log(f(1, 2));
var f = function(a, b) {
    return a * b;
}
//报错,TypeError: f is not a function

这样就不报错了

console.log(f(1, 2));
function f(a, b) {
    return a * b;
}
//不报错,输出2
原文地址:https://www.cnblogs.com/huwt/p/10703264.html