My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
property  
Overview of property
Updated Nov 15, 2010 by matr...@mac.com

OProperty or objx().property

Adds an OO property to an object.

Usage

OProperty(object, propertyName [, value]);

or

objx(object).property(propertyName [, value]);

object The object to add the property to
propertyName string containing the name of the property
value (optional) The initial value to set the property to

Overview

OProperty or .property() adds the following items to the object:

  • An instance variable to hold the value of the property (e.g. obj._propertyName)
  • A shortcut method for the property (e.g. obj.propertyName())
  • An explicit getter method (e.g. obj.getPropertyName())
  • An explicit setter method (e.g. obj.setPropertyName(value))
  • A remove helper method for the property (e.g. obj.propertyName.remove())

Instance variable

The 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

obj The object with the property
propertyName The name of the property.

Shortcut method

A 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 value

obj.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 value

To 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 function

If 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 getter

The 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 setter

The setter method sets the obj._propertyName variable to a new value.

obj.setPropertyName(value);

value The new value of the property

Overriding the getters and setters

If 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 property

OProperty provides a clean way of removing the property from an object too:

obj.propertyName.remove();

obj The object that the property was applied to
propertyName The name of the property (actually the shortcut method)

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

  • See the OClass plugin for how to use properties in a true object-oriented way

Tutorials


Sign in to add a comment
Powered by Google Project Hosting