【发布时间】:2018-10-23 02:20:18
【问题描述】:
我有一个 IIFE,里面有一个嵌套函数。嵌套函数内部和外部都有一个 word 变量。如何访问嵌套函数外部而不是内部函数的word 变量?
代码:
(function (){
let word = "Hello";
function sayHello(){
let word = "Greetings";
console.log(word + " Everyone!"); // This is using the inside word variable instead of the outside one. How can I specify JS to use the outside one instead?
}
sayHello();
})();
【问题讨论】:
-
要直接联系他们,必须重命名其中之一。相关:Variable shadowing 和 JavaScript Access Local Variable with Same Name in Inner and Outer Scope
标签: javascript scope iife