执行上下文两次声明名称
execution context declared name twice?
我正在研究Javascript中的执行上下文。但我不明白为什么"函数foo"不会被"var foo"重写。我希望有人能解释一下,感谢您的回复。
函数ace(){
console.log(typeof foo); // function pointer
console.log(typeof bar); // undefined
var foo = 'hello',
bar = function() {
return 'world';
};
function foo() {
return 'hello';
}
var foo = 'hello';
}
ace();
为什么"函数foo"不会被"var foo"重写。
foo
在foo
被重新定义的行被重写
function ace() {
console.log(typeof foo); // function pointer
console.log(typeof bar); // undefined
var foo = 'hello',
bar = function() {
return 'world';
};
function foo() {
return 'hello';
}
var foo = 'hello';
console.log(foo);
try {
foo()
} catch(e) {
console.log("catch:", e.message)
}
}
ace();
但我不明白为什么"函数foo"不会被"var foo"重写。
var
声明不会覆盖函数声明。它们都声明了相同的变量,并且由于函数声明,它是用函数初始化的。只有赋值才会覆盖该值。
如果你考虑吊装,你的脚本将表现得像
function ace() {
var foo, bar;
var foo = function() {
return 'hello';
}
var foo;
console.log(typeof foo); // function
console.log(typeof bar); // undefined
foo = 'hello';
bar = function() {
return 'world';
};
console.log(typeof foo); // string
foo = 'hello';
}
ace();
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- Module.start()已激发两次
- Jquery点击事件必须点击两次
- Append元素在运行两次函数后不显示
- 防止双击执行两次jQuery post请求
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 单击jQuery会激发两次
- 如何避免在树上走两次
- button.单击两次删除附加操作后不工作
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 单击元素两次后执行操作
- AngularJs正在阻止链接被点击两次
- 为什么我的路线处理程序会触发两次
- 为什么要声明两次 jQuery
- 为闭包声明函数两次
- 为什么要两次声明同一个变量的相同值
- 执行上下文两次声明名称
- javascript函数的声明两次
- 我可以在JavaScript中的不同for循环中两次声明同一个变量吗