为什么下面代码的结果是 1 而不是 10?为什么函数 bar() 不更改值,即使有 return 语句。
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {}
}
bar();
alert(foo);
最佳答案
这是因为 function
定义被提升到它们作用域的顶部,所以 bar()
中的 foo 赋值实际上指的是具有空值的局部函数 body 。
您可以阅读更多相关信息 here
关于javascript - 什么在函数中创建函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40203333/