首先定义个函数,如下:
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

解决方案 »

  1.   

    this.SetA = function (a) {
                    a = a;
        }
    关键在这 SetA 上,这里 a 不是上面的a, 本域里形参上的那个a
      

  2.   

    您好,如果我将函数,修改为这样:
     function test(a) {
                var a = a;
    //            console.log(a);            this.GetA = function () {
                    return a;
                };            this.SetA = function (a) {
                    a = a;
                }
            }测试结果就是:1,2,3.这是为什么呢,实在是想不通
      

  3.   

    3楼是写错了,应该这样修改,结果就是:1,2,3了。
      function test(a) {
                var b = a;
    //            console.log(a);            this.GetA = function () {
                    return b;
                };            this.SetA = function (a) {
                    b = a;
                }
            }