Login Register Help
API Documentation
Choose a stylesheet:

Functiondojo.declare (returns Function)

Create a constructor using a compact notation for inheritance and prototype extension.

All superclasses (including mixins) must be Functions (not simple Objects).

Mixin ancestors provide a type of multiple inheritance. Prototypes of mixin ancestors are copied to the new class: changes to mixin prototypes will not affect classes to which they have been mixed in.

“className” is cached in “declaredClass” property of the new class.

Usage

var foo: Function=dojo.declare(className: String, superclass: Function|Function[], props: Object);
parametertypedescription
classNameStringThe name of the constructor (loosely, a "class") stored in the "declaredClass" property in the created prototype
superclassFunction|Function[]May be null, a Function, or an Array of Functions. If an array, the first element is used as the prototypical ancestor and any following Functions become mixin ancestors.
propsObjectAn object whose properties are copied to the created prototype. Add an instance-initialization function by making it a property named "constructor".

Examples

Example 1

Declare a class with no ancestors.

dojo.declare("my.ClassyThing", null, {
    aProperty:"string",
    constructor: function(args){
        dojo.mixin(this, args);
    }
});

Example 2

Declare a class inheriting from my.classed.Foo

dojo.declare("my.classes.Bar", my.classes.Foo, {
    // properties to be added to the class prototype
    someValue: 2,
    // initialization function
    constructor: function(){
        this.myComplicatedObject = new ReallyComplicatedObject();
    },
    // other functions
    someMethod: function(){
        doStuff();
    }
);

Example 3

Declare a class inherting from two mixins, handling multiple constructor args

dojo.declare("my.ComplexMix", [my.BaseClass, my.MixedClass],{
    constructor: function(a, b){
        // someone called new my.ComplexMix("something", "maybesomething");
    }
});

Jump to FunctionsNamespacesBack to top

Jump to NamespacesFunctionsBack to top