dev*_*os1 7 javascript closures scope namespaces
在内部函数中定义的变量是否与外部函数中与外部变量隔离的变量具有相同的名称?
function() {
var myTest = "hi there";
( function( myTest ) {
myTest = "goodbye!";
} )();
console.log( myTest ); // myTest should still be "hi there" here, correct?
}
Run Code Online (Sandbox Code Playgroud)
当然,如果我没有在内部函数内部声明 myTest
,它将创建一个闭包并修改原始函数.我只是想确保在内部函数中声明的变量始终与该函数隔离,即使它们的名称可能与外部作用域冲突.