sand-extend
sand-extend offers a simple way to have class inheritance in javascript. It works well with and without the sand framework.
Currently sand-extend offers 2 ways to do inheritance:
- Class Based method with construct (Full inheritance)
- Extend Based method with prototype inheritance (has some caveats)
Class Based
Class based inheritance is a little different then what you are used to but offers full inheritance concept
var Animal = Class; var Dog = Animal; var Collie = Dog;
Extend Based
Extend based uses the typical prototype inheritance but adds the super method so you can call the parent.
Example
; { thisname = name || 'No Name';} Animalprototype { return 'My name is ' + thisname;}; { this;} // Extend the Animal classDog; { this;} // Extend the Animal classCat; var dog = 'Terrie';var cat = 'Hana'; // Prints out `My Name is Terrie and I Bark`console; // Prints out `My Name is Hana and I Meow`console;
Caveats
Extend supports multiple levels of inheritance but you can't call this.super() in all constructors as it has an infinite loop issue. this.super() can still be called in all methods though, an infinite number deep.
OMG It Extends Function?
Yes, yes it does, with a single getter extend, and no it won't break your code, because it does this properly with a non-enumerable property.
Also it only does this if you enable it with enableGlobalExtend
. It is off by default.
To begin
-
Install it:
$ npm install sand-extend -S -
Require it and use:
// Either enable Global extend;// or Use extend by itselfvar Extend = Extend;
Tests
To run the tests for sand-extend simply run:
$ npm test
License
ISC © 2014 Pocketly