class.js
A super small ruby-ish class system with both class and class instance inheritance.
Example usage:
var Animal = Class; var Mammal = Animal; var HasLegs = { console; }; var Biped = Mammal; var Quadruped = Mammal; Quadruped; var Kangaroo = Biped; var Dog = Quadruped; var jumpy = Kangaroo;jumpy; var sparky = Dog;
Class.js does not use constructors
//=> TypeError: object is not a function
The reason Animal
is not a Function
is because functions cannot be created with a prototype other than Function.prototype
.
Extend
Both the Class
and Class#prototype
objects have an extend function that supports plain objects as well as functions
var Kitten = Class; Kitten; KittendefaultFurColor; //= 'orange' Kitten; KittendefaultFurColor; //= 'brown' Kittenprototype; Kittenage; //= 1 Kittenprototype; Kittenage; //= 12
subclass
When you call Animal.subclass()
it returns a new object that points to Animal
. The new object's prototype property is also a new object that points to Animal.prototype
.
modules
A Module can either be a plain object or a function.
var Car = Class; var HasEngine = { if !this_engine this_engine = Engine; return this_engine; }; var { { // localStorage[…] = …; return this; } { // return this.new(localStorage[…]); } return { thisclassfind = find; thissave = save; }; }; Car; typeof Carfind //= 'function'typeof Carsave //= 'function'typeof Carengine //= 'function'
Tests
npm install mochanpm install expect.jsmocha -R spec