Built-In Namespace _global_
Method Attributes | Method Name and Description |
---|---|
$abstract(method)
Creates an abstract method in a class. |
|
$final(method)
Creates a final method in a class, or creates a final class. |
|
$static(value)
Creates a static property or method in a class or interface. |
Creates an abstract method in a class. This is a property modifier for use within a $class.descriptor.
An abstract method has no implementation and prevents the class from being instantiated. A class with any abstract methods is inherently abstract; the class itself does not need to be (and cannot be) marked as absract. Any class that extends an abstract class is also abstract, unless it implements (overrides) all inherited abstract methods. The abstract modifier may only be applied to non-$final and non-$static methods.
Debug version errors:
- Attempt to instantiate an abstract class. The error message will indicate the abstract class and will list all abstract methods and from which class they were inherited.
- Attempt to create an abstract property (not a method). The error message will indicate the class and property name.
Example:
// abstract class that implements some algorithm, // but leaves some details unspecified $class("AbstractBase", { // returns an array of numbers getData: $abstract(function() {}), // "protected" implementation detail of some algorithm. _shouldAddValue: $abstract(function(value) {}), // the algorithm that uses the abstract methods performSomeAlgorithmWithData: $final(function() { var data = this.getData(); var result = 0; for (var i = 0; i < data.length; ++i) { if (this._shouldAddValue(data[i]) { result += data[i]; } } return result; }) }); // class that implements the abstract methods $class("Implementation", { $extends: AbstractBase, $constructor: function(data) { this._data = data; }, getData: function() { return this._data; }, _shouldAddValue: function(value) { return value > 0; } }); var abstractBase = new AbstractBase(); // ERROR! var impl = new Implementation([1, -5, 9, -10, 5]); alert(impl.performSomeAlgorithmWithData()); // alerts "15"
Defined in: class-debug.js.
- Parameters:
- {Function} method
- A function. The function itself is not actually used, but it must be provided. This both allows for a sanity check that you aren not trying to create an abstract property (can't be done) and provides a convenient place for you to declare/document the arguments.
- Returns:
- {Object} Something magical that, when assigned to a property of a $class.descriptor, causes an abstract method to be created.
Creates a final method in a class, or creates a final class. This is a property modifier for use within a $class.descriptor.
A final method is not allowed to be overridden by descendent classes. The final modifier can also be applied to the $class.descriptor#$constructor of a class to prevent the class from being overridden althogether. The final modifier may only be applied to non-$abstract and non-$static methods.
Debug version errors:
- Attempt to extend a class whose constructor is final.
- Attempt to override an inherited final method.
Example:
// extend this class to inherit a unique ID $class("UniquelyIdentifiable", { $constructor: function() { this._id = "UniquelyIdentifiable_" + (UniquelyIdentifiable._idCounter++); }, // If this method were ever overridden to do something different, it could // break the contract of this class (promises a unique ID). Making it final // keeps it completely within control of this class. getId: $final(function() { return this._id; }), _idCounter: $static(0) });
Defined in: class-debug.js.
- Parameters:
- {Function} method
- The implementation of the final method.
- Returns:
- {Object} Something magical that, when assigned to a property of a {@link $class.descriptor), causes a final method to be created.
Creates a static property or method in a class or interface. This is a property modifier for use within a $class.descriptor or $interface.descriptor.
A static property/method is accessible as a property of the class or interface without having an instance. Within static methods, the "this" keyword will refer to the class or interface to which the static method belongs. Static methods/properties are not inherited; you must access them on the exact class or interface in which they were defined. Note that static methods by definition cannot be made $abstract or $final.
Example:
$class("Sample", { $constructor: function(name, type) { this._name = name; this._type = type; }, equalTo: function(other) { return other && other instanceof Sample && other._name == this._name && other._type == this._type; }, // static constants for indicating the "type" Type: $static({ GOOD: 1, BETTER: 2, BEST: 3 }), // static factory method createBestSample: $static(function(name) { // "this" in static methods refers to the class/constructor return new this(name, this.Type.BEST); }) }); var sample1 = new Sample("Bob", Sample.Type.BEST); var sample2 = Sample.createBestSample("Bob"); alert(sample1.equalTo(sample2)); // alerts "true"
Defined in: class-debug.js.
- Parameters:
- {mixed} value
- Any value. May be a function.
- Returns:
- {Object} Something magical that, when assigned to a property of a $class.descriptor or $interface.descriptor, causes a static property to be created.