我注意到公共内部函数和私有内部函数中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
都不同?