JavaScript:内部函数私有与公共

时间:2015-05-10 00:03:18

标签: javascript

我注意到公共内部函数和私有内部函数中this的行为不同。

(function () {
    "use strict";

    function MyObject(){
        var myThis = this;
        var z = function(){
            console.log("Item",myThis);     
        };
        var y = function(){
           console.log("Item",this);
        };
        this.x = function(){
            console.log("Item",this);
        };
        this.out = function(){
            y();
            this.x();
        };
    }
    var myObject = new MyObject();
    myObject.out();
}());

输出

"Item"
[object Object] {
  out: function (){
"use strict";

      z();
      y();
      this.x();
    },
  x: function (){
"use strict";

      window.runnerWindow.proxyConsole.log("Item",this);
    }
}
"Item"
undefined
"Item"
[object Object] {
  out: function (){
"use strict";

      z();
      y();
      this.x();
    },
  x: function (){
"use strict";

      window.runnerWindow.proxyConsole.log("Item",this);
    }
}

为什么两个this都不同?

0 个答案:

没有答案