与此参考资料相同,函数声明语句和变量声明不具有块级作用域。因此,我想知道下面的代码为什么会以这种方式执行。
我知道的是上面的代码应该考虑全局作用域中的两个函数,但似乎函数声明受块级作用域的影响。
//javascript es6
{
function bar() {
console.log("1");
}
}
function bar() {
console.log("2");
}
bar(); //prints "1" not "2"
this.bar() //also prints "1" not "2"
this.bar()
在 FF 中触发错误(包括window.bar()
)(“_TypeError:this.bar不是一个函数_”)。 - Teemu