【发布时间】:2013-04-01 17:58:03
【问题描述】:
使用 Fabric.js 1.1.6,我有以下内容:
var rect = new fabric.Rect({});
rect.on('moving', function(obj) {
// I want to retrieve and set properties from the
// object rect from inside this event function
});
- 是否可以从事件调用的函数内部检索和修改
rect属性? - 如果是,我该怎么做?
- 如果不是,关于如何从事件函数内部修改 rect 属性的任何建议?
提前谢谢你!
--
更新:@plalx 正确回答了上述问题,但实际上我在更大的背景下有这个问题:
function SomeClass()
{
this.rect = new fabric.Rect({});
this.rect.on('moving', function(obj) {
// here, this.rect is undefined
});
}
好吧,在后一种情况下,我不能只在函数内部使用 this.rect,因为 this 指的是函数,而不是 SomeClass。
现在怎么样,以上三个问题的答案是什么?
【问题讨论】:
-
如果一个函数在创建时引用了范围内的某些变量,它将“关闭”这些变量。这意味着它们将可供它访问和修改,即使它们在您调用它时不在范围内。
标签: javascript events fabricjs