Crafty.defineField()

public void Crafty.defineField(Object object, String property, Function getCallback, Function setCallback)
object

要定义属性的对象

property

要分配 getter&setter 方法的属性名称

getCallback

当属性被获取时的回调函数

setCallback

当属性被设置时的回调函数。(属性值变化时方法被执行)

为对象的属性指定 getters和setters方法。getter方法将监视属性访问,当属性被访问时 getCallback 函数将会被执行。setter方法将会监视属性设置,当属性被改变时 setCallback 方法将会被执行。

例子

var ent = Crafty.e("2D");
Crafty.defineField(ent, "customData", function() {
   return this._customData;
}, function(newValue) {
   this._customData = newValue;
});

ent.customData = "2" // set customData to 2
Crafty.log(ent.customData) // prints 2