【发布时间】:2016-08-28 08:26:07
【问题描述】:
var Shape = function(type)
{
this.type = type;
addEventListener("resize", this.align);
}
Shape.prototype.align = function()
{
alert(this.type);
}
.
var variable = new Shape('rectangle');
当我调整大小时,我想要提醒 rectangle 但它提醒 undefined
【问题讨论】:
-
alert(this)(或者更确切地说是记录到控制台),你看到了什么? -
添加到 BenG 的答案。在事件下,this 将指向 window
-
我看到了[Object object]
-
那是因为alert,试试console.log而不是alert
标签: javascript methods javascript-objects dom-events