UIZE JavaScript Framework

2012 NEWS 2012-12-31 - Uize.Class Gets Feature Declaration Methods

The Uize.Class base class has been improved with the addition of several new feature declaration methods that let you declare instance and/or static features for a class.

1. Feature Declaration Methods

The new feature declaration methods are as follows...

Uize.Class.instanceMethods - lets you declare one or more instance methods for the class
Uize.Class.instanceProperties - lets you declare one or more instance properties for the class
Uize.Class.staticMethods - lets you declare one or more static methods for the class
Uize.Class.staticProperties - lets you declare one or more static properties for the class
Uize.Class.dualContextMethods - lets you declare one or more dual context methods for the class
Uize.Class.dualContextProperties - lets you declare one or more dual context properties for the class

For comprehensive documentation on these new methods, consult the reference for the Uize.Class module.

2. Declare Private or Public Features

The feature declaration methods can be used either to declare public features or private features.

In UIZE, there is no fundamental difference between private methods or properties and public methods or properties - it's all in the naming. By convention, private features are named with an "_" (underscore) prefix. This has its pros and cons, but one side effect of this is that either private or public features (or a mixture of both) can be declared using the feature declaration methods.

EXAMPLE

_class.instanceMethods ({
  _privateInstanceMethod1:function () {
    // implementation here
  },
  _privateInstanceMethod2:function () {
    // implementation here
  },
  publicInstanceMethod1:function () {
    // implementation here
  },
  publicInstanceMethod2:function () {
    // implementation here
  }
});

In the above example, one call to the Uize.Class.instanceMethods method is being used to declare the _privateInstanceMethod1 and _privateInstanceMethod2 private instance methods, along with the publicInstanceMethod1 and publicInstanceMethod2 public instance methods.

3. Feature Declarations are Cumulative

All the feature declaration methods can be called as many times as desired, and calling them repeatedly is cumulative in nature.

This is useful, because it lets you break out declarations into different sections in your code if that makes your code more readable and/or manageable.

EXAMPLE

// Private Instance Methods
_class.instanceMethods ({
  _privateInstanceMethod1:function () {
    // implementation here
  },
  _privateInstanceMethod2:function () {
    // implementation here
  }
});

// ... ... ... ... ... ... ... ... ...

// Public Instance Methods
_class.instanceMethods ({
  publicInstanceMethod1:function () {
    // implementation here
  },
  publicInstanceMethod2:function () {
    // implementation here
  }
});

In the above example, the Uize.Class.instanceMethods method is being called twice - in one section to declare private instance methods, and in the other section to declare public instance methods.

4. Add or Override Features

The feature declaration methods can be used either to add features that aren't inherited from the class' base class, or to override features that are inherited from the base class.

5. Dual Context

Dual context class features are features that exist both on the class as well as instances of the class.

Examples dual context features are the various event system methods. For example, the fire instance method lets you fire an instance event, while the Uize.Class.fire static method lets you fire an event on a class. Both the instance and class methods for firing events share the same underlying implementation, where the implementation may contain minor conditionalizing when executing in the instance context versus executing in the class context.

In cases where it is possible (and possibly even desirable) to share the same function between an instance method and a class method, the Uize.Class.dualContextMethods static method can be used to declare such methods in a single statement, rather than separately calling both the Uize.Class.instanceMethods and Uize.Class.staticMethods methods.

Although a less likely scenario, it is also possible to declare dual context properties using the Uize.Class.dualContextProperties static method. This method is present mainly for symmetry and consistency.

For dual context features, it is assumed that the feature is named the same on both the instance and the class. In situations where this is not the case, one should just use the separate methods for defining instance features and class features.