【发布时间】:2012-03-26 03:02:22
【问题描述】:
内部函数中定义的变量是否与外部函数中的变量同名,与外部变量隔离?
function() {
var myTest = "hi there";
( function( myTest ) {
myTest = "goodbye!";
} )();
console.log( myTest ); // myTest should still be "hi there" here, correct?
}
如果我没有在内部函数中声明myTest,自然会创建一个闭包并修改原始函数。我只是想确保在内部函数中声明的变量始终与该函数隔离,即使它们的名称可能与外部范围冲突。
【问题讨论】:
标签: javascript namespaces scope closures