我在理解 js 的工作原理方面遗漏了一些东西。这是问题所在:
我们声明一个这样的模块:
ns.obj = function() {
// declare private variables
var test = 1, test1 = 2;
// declare some private function
var myFunc=function(){test=2};
return{test:test, myFunc:myFunc};
}
每次myFunc
调用,因为我们没有在函数内声明 test js 应该假设我们指的是私有变量。
如果我们有模块,返回的对象确保我们有 test 和 myFunc 可见。所以调用 ns.obj.test 应该首先给我们 1。在我们调用 myFunc 之后应该给我们 2。但它总是 1。为什么会发生这种情况?
这是jsfiddle:http: //jsfiddle.net/aXuwB/1/