A bit of sugar for defining Objects and their prototypes.
- Define objects without having to deal with the awkward and verbose syntax found in ES3 & ES5.
- Painless inheritance, initialization and static methods of the parent class are automatically carried over to the child class.
- After inheritance, the constructor property on the prototype correctly points to it's constructor, unlike common js inheritance patterns.
- Provides a simple mechanism for mixins.
//Create a base class with some default methodsvar Base = defineObjectconsole.log"hello world!";;//Extend from the base classvar Animal = Baseextend//a initialization method with side-effects//that luckily will not get called when the Animal object is extended fromthislaunchRockets;sound: 'silence'weight: 0console.logthissound;;//Create a logger method which will be used as a mixin in our example//any object, or constructor can be used as a mixinvar Logger = BaseextendthislogHistory = ;thislogHistorypushmsg;console.logmsg;//Define the Dog object extending from Animal while mixing in Logger.var Dog = Animalextendthisweight = weight;//Dog.__super.__init__.call(this); -- if you need to have access to the Animal initialization methodsound : 'woof'Dog__super__vocalizecallthis; // Calling 'super' methods remains about the same.this.log"dog is vocalizing";console.log"I want what you're having.";this.log"dog is drooling"mixinLogger;var dog = ;doghello;dogvocalize;dogdrool;console.log"History" doglogHistory;console.log dog instanceof Dog ; //trueconsole.log dog instanceof Animal; //trueconsole.log dogconstructor === Dog; //trueconsole.log dog instanceof Logger; //false
varthisfoo = 'bar';;'foo';var Ext = defineObjectextendPlainfizz: 'fuzz'mixin'fud' : 'foo';var obj = Extcreate;console.logobj instanceof Plain;console.logobjfoo objbar objfizz objfud;
Mixins can take the form of either an object or a constructor.
if an object is used the object properties are merged into the target prototype, if an init method is defined than it will on instantiation.
var O = defineObjectmixinthismsg = "hello world!";console.logthismsg;mixinthisvalue = "hello earth!";console.logthisvalue;;var o = ;oprint //"hello world!";oshow //"hello earth!";
If a constructor is passed into the mixin method, the init method on the prototype will be used upon instantiation. If an init method does not exists, the constructor method itself will be used.
Static methods can be defined by passing in a second object literal to defineObject or extend;
var O = defineObjectinstanceProp: 'foo'staticProp: 'bar';console.logOstaticProp
Note: defineOjbect will not allow you to redefine existing static properties, an exception will be thrown.
calling the 'properties' method on an object definition will simply pass the assigned literal to Object.defineProperties upon the creation of an object.
var O = defineObjectpropertiesfield :value : 'value'writable : false;var o = Ocreate;console.logofield;ofield = 'error'; //error
Objects can be defined in a more chainable style as well:
Dog = Animalextendmethodsstaticsmixinmixinproperties
Where 'methods' and 'statics' replace the first two arguments of the extend/defineOjbect function. Both functions can also be used to extend the prototype/static methods of an Object.