rsk*_*k82 2 javascript scope chaining
例:
function testFunc() {
this.insideFunc = function(msg) {
alert(msg);
}
return this;
}
testFunc().insideFunc("Hi!");
insideFunc("Hi again!");
Run Code Online (Sandbox Code Playgroud)
为什么内部函数在全局范围内可见,如何防止?
这是因为this是window.
要this以这种方式使用,您必须使用:
var _testFunc = new testFunc();
Run Code Online (Sandbox Code Playgroud)