假設的jQuery:jQuery的調整大小失去「這個」參考包括
function FixedPoint(bgWidth) {
this.bgWidth= bgWidth;
this.plot();
$(window).resize(this.plot);
}
FixedPoint.prototype.plot = function() {
console.log(this.bgWidth); //This is undefined when called by resize
}
var pt1 = new FixedPoint(1920);
當圖()被調用構造函數或初始化後一切正常,但是當圖()正在被調整大小稱爲函數,它不能再通過'this'來訪問實例變量。
我可以調用構造函數的外側來修復此問題,但希望在類中保持整齊。
是的,這是預期的。當這個函數作爲事件的回調傳遞時,this裏面是事件的目標。 –