求助一个 运行结果的疑问
首先定义个函数,如下:
function test(a) {
var a = a;
// console.log(a);
this.GetA = function () {
return a;
};
this.SetA = function (a) {
a = a;
}
}测试函数,如下:
function playTest() {
var t = new test(1);
console.log(t.GetA());
t.SetA(2);
console.log(t.GetA());
t.SetA(3);
console.log(t.GetA());
}问题:
为什么运行结果是:1,1,1呢?
为什么不是1,2,3呢,我也知道闭包,但是这个结果,无法理解,请赐教 js
[解决办法]
this.SetA = function (a) {
a = a;
}
关键在这 SetA 上,这里 a 不是上面的a, 本域里形参上的那个a