Crafty.defineField()

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

Object to define property on

property

Property name to assign getter & setter to

getCallback

Method to execute if the property is accessed

setCallback

Method to execute if the property is mutated

Assigns getters and setters to the property in the given object. A getter will watch a property waiting for access and will then invoke the given getCallback when attempting to retrieve. A setter will watch a property waiting for mutation and will then invoke the given setCallback when attempting to modify.

Example

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