Declare is a library designed to allow writing object oriented code the same way in both the browser and node.js.
Installation
npm install declare.js
Or download the source (minified)
Requirejs
To use with requirejs place the declare
source in the root scripts directory
;
Usage
declare.js provides
Class methods
as(module | object, name)
: exports the object to module or the object with the namemixin(mixin)
: mixes in an object but does not inherit directly from the object. Note this does not return a new class but changes the original class.extend(proto)
: extend a class with the given properties. A shortcut todeclare(Super, {})
;
Instance methods
_super(arguments)
: calls the super of the current method, you can pass in either the argments object or an array with arguments you want passed to super_getSuper()
: returns a this methods direct super._static
: use to reference class properties and methods.get(prop)
: gets a property invoking the getter if it exists otherwise it just returns the named property on the object.set(prop, val)
: sets a property invoking the setter if it exists otherwise it just sets the named property on the object.
Declaring a new Class
Creating a new class with declare is easy!
var Mammal = ;
You can use Mammal just like you would any other class.
Mammal; var myMammal = type : "mymammal";myMammal; // "A mammal of type mymammal sounds like"myMammal; //"mymammal"myMammal;myMammal; //"mammal"
Extending a class
If you want to just extend a single class use the .extend method.
var Wolf = Mammal; Wolf; //Im a mammal!! that growls var myWolf = ;myWolf instanceof Mammal //truemyWolf instanceof Wolf //true
You can also extend a class by using the declare method and just pass in the super class.
//Typical hierarchical inheritance// Mammal->Wolf->Dogvar Dog = ; Dog; //Im a mammal!! that growls but now barks var myDog = ;myDog instanceof Mammal //truemyDog instanceof Wolf //truemyDog instanceof Dog //true //Notice you still get the extend method. // Mammal->Wolf->Dog->Breedvar Breed = Dog; Breed//"IM A MAMMAL!! THAT GROWLS BUT NOW BARKS!" var myBreed = color : "gold" type : "lab"myBreed instanceof Dog //truemyBreed instanceof Wolf //truemyBreed instanceof Mammal //truemyBreed //"A mammal of type lab sounds like a woof thats domesticated with a high pitch!"myBreed //"lab"myBreed //"gold"myBreed" //"woof"
Multiple Inheritance / Mixins
declare also allows the use of multiple super classes. This is useful if you have generic classes that provide functionality but shouldnt be used on their own.
Lets declare a mixin that allows us to watch for property changes.
//Notice that we set up the functions outside of declare because we can reuse them { //get the old value var oldVal = this; //call super to actually set the property var ret = this; //call our handlers this; return ret;} { //get our handlers for the property var handlers = this__watchersprop l; //if the handlers exist and their length does not equal 0 then we call loop through them if handlers && l = handlerslength !== 0 for var i = 0; i < l; i++ //call the handler handlersi; } //the watch function { if "function" !== typeof handler //if its not a function then its an invalid handler throw "Invalid handler."; if !this__watchersprop //create the watchers if it doesnt exist this__watchersprop = handler; else //otherwise just add it to the handlers array this__watchersprop; } { if "function" !== typeof handler throw "Invalid handler."; var handlers = this__watchersprop index; if handlers && index = handlers !== -1 //remove the handler if it is found handlers; }
Now lets use the mixin
var WatchDog = ; var watchDog = ;//create our handler { console;} //watch for property changeswatchDog;watchDog;watchDog; //now set the properties each handler will be calledwatchDog;watchDog;watchDog; //unwatch the property changeswatchDog;watchDog;watchDog; //no handlers will be called this timewatchDog;watchDog;watchDog;
Accessing static methods and properties witin an instance.
To access static properties on an instance use the _static
property which is a reference to your constructor.
For example if your in your constructor and you want to have configurable default values.
{ thisopts = opts || {}; this_type = optstype || this_staticDEFAULT_TYPE;}
Creating a new instance of within an instance.
Often times you want to create a new instance of an object within an instance. If your subclassed however you cannot return a new instance of the parent class as it will not be the right sub class. declare
provides a way around this by setting the _static
property on each isntance of the class.
Lets add a reproduce method Mammal
{ return options;}
Now in each subclass you can call reproduce and get the proper type.
var myDog = ;var myDogsChild = myDog; myDogsChild instanceof Dog; //true
as
Using the declare
also provides an as
method which allows you to add your class to an object or if your using node.js you can pass in module
and the class will be exported as the module.
var animals = {}; Mammal;Wolf;Dog;Breed; var myDog = ;
Or in node
Mammal;Wolf;Dog;Breed;
To export a class as the module
in node
Mammal;