繁体   English   中英

有人可以解释以下代码的输出吗?

[英]Can someone explain the output of following code?

 var employeeId = 'abc123'; function foo() { employeeId = '123bcd'; return; function employeeId() { var x = 0; } } foo(); console.log(employeeId); //abc123


问题是基于提升。 我们在这里在 console.log 中打印 employeeId。 有人可以解释上面代码的输出吗?

根据提升规则,在同一个闭包中, function声明几乎等价于:

 var employeeId = 'abc123'; function foo() { var employeeId = function () { // hoisted from the function below var x = 0; } employeeId = '123bcd'; return; // function employeeId() { // var x = 0; // } } foo(); console.log(employeeId); //abc123

所以外部作用域employeeId没有被改变,因为它在foo函数作用域中被遮住了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM