在进行链接时如何使内部功能不超出主要功能范围?

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)

为什么内部函数在全局范围内可见,如何防止?

pdo*_*926 5

这是因为thiswindow.

this以这种方式使用,您必须使用:

var _testFunc = new testFunc();
Run Code Online (Sandbox Code Playgroud)