|
property
Overview of property
OProperty or objx().propertyAdds an OO property to an object. UsageOProperty(object, propertyName [, value]); or objx(object).property(propertyName [, value]);
OverviewOProperty or .property() adds the following items to the object:
Instance variableThe object will have its own instance variable which will store the value of the property for that object. It will be the name of the property prefixed with an underscore (_). obj._propertyName
Shortcut methodA handy shortcut method will also be added with the same name as the property which can be used to handle most cases for dealing with the property. The following overviews assume you have created a new property called name on the object obj (with the default value set to "Mat": objx(obj).property("name", "Mat");Setting the property valueobj.name("Mat");
// sets the property value to "Mat"The return of the shortcut method when used as a setter is the object itself, which means you can go on to set more properties or call other methods: obj.name("Mat").age(28).doSomething();Getting the property valueTo get the value of the property you can use the shortcut method without any arguments: obj.name(); The getter also allow you to pass an additional behaviour modifier keyword (as the last argument) to the function to change its behaviour: // get name and pass it to a callback
obj.name(function(n){ alert("Name is " + n); });
// returns obj
// alerts "Name is Mat"
// get name and return an objx instance containing the value
obj.name(ONew);
// returns objx("Mat");Setting the value of a property to a functionIf you try to set a property to a function (i.e. so that obj._propertyName = function(){}) via the shortcut method, it will be interpreted as a getter with a callback. To avoid confusion, you should use the explicit getter (see below). Explicit getterThe getter method returns the current value of the property. obj.getPropertyName(); The explicit getter also supports passing a callback function or ONew to modify the behaviour. See above. Explicit setterThe setter method sets the obj._propertyName variable to a new value. obj.setPropertyName(value);
Overriding the getters and settersIf you explicitally specify a getter or setter method in the object, they will be used instead of the built in versions. However, the chaining and action functionality will be persisted. The following code will ensure the value for propertyName is stored internally in lowercase, but expressed externally in uppercase. var obj = {
getPropertyName: function() {
return this._propertyName.toUpperCase();
},
setPropertyName: function(value) {
this._propertyName = value.toLowerCase();
}
};
// add the property
objx(obj).property("propertyName");
// set the value
obj.propertyName("Mat Ryer");
// obj._propertyName == "mat ryer"
// get the value
obj.propertyName();
// returns "MAT RYER"Removing the propertyOProperty provides a clean way of removing the property from an object too: obj.propertyName.remove();
All traces of the property will be removed if you call this method, including the remove method itself, along with the getter, setter, shortcut method and the property value. Properties in classes
Tutorials | ||||||||||||||||