wheels-class can be installed with CommonJS / npm, AMD or standard browser script tag:
npm install wheels-class
AMD / RequireJS
require"wheels-class"var MyClass = ;;
Traditional browser script tag
Pass an object to the constructor to create a class and add the object's properties to the class
var Foo =return "Hello :)";;var foo = ;foogreet; // => "Hello :)"
Alternatively, you can pass a function to the constructor, and it will be executed in the scope of the class. That means that, whithin the function,
this is the class itself. Also, the function will receive the
prototype property of the class as the first argument:
var Foo =// `this` is the class, here Fooreturn "I am a class method";// first argument is the class `prototype` property (here Foo.prototype)return "Hello :)";;var foo = ;FooclassMethod; // => "I am a class method"fooinstanceMethod; // => "Hello :)"
subclass method creates a subclass and accepts the same arguments as the constructor:
var Animal =return "Yum :)";;var Cat = Animalsubclassreturn "Meow!";;var nyan = ;nyaneat; // => "Yum :)"nyanmeow; // => "Meow!"// The subclass also stores a reference to the superclassCat_superclass === Animal // => true// And the instance stores a reference to the parent prototypenyan_parent === Animalprototype // => true
include method accepts one or more objects and copies their properties to the class'
var Duck = ;Duckincludereturn "Quack!";;var donald = ;donaldquack; // => "Quack!"
augment method accepts one or more objects and copies their properties to the class:
var Foo = ;Fooaugmentreturn "I am a class method!";;FooclassMethod; // => "I am a class method!"
reopen method accepts the same arguments as the
Class constructor: if you pass an object, its properties get added to the class prototype, if you pass a function it is executed in the scope of the class, passing the prototype as the first argument.
var Human =john = ;// Passing an objectHumanreopenreturn "Goo goo goo joob!";;johnsing; // => "Goo goo goo joob!"// Passing a functionHumanreopen// `this` is the class, here Humanreturn "ergo sum.";;// proto here is Human.prototypereturn "Yum!";;Humancogito; // => "ergo sum."johneat; // => "Yum!"