Define Classes in Javascript
This is a simple module providing a simple Class
function to simplify class definition in Javascript.
Install
For Node.js
npm install js-class
For browser, use bower
bower install js-class
To rebuild js-class.min.js
, type:
npm installnpm run-script build
How to Use
With Node.js:
var Class = ;
In browser, just include js-class.min.js
Prototype
;
Parameters
baseClass
: baseClass type, optional, default is Object;prototype
: the prototype for new class;options
: other options like implements and statics, see details below.
Returns
The newly defined class.
Details
A simple and quick sample:
var Class = ; var MyClass = ; var myObject = 1;myObject; // get 1myObjectvalue = myObjectvalue + 100;myObject; // get 101
A simple inheritance sample:
var BaseClass = ; var SubClass = ; var myObject = 1 2;myObject instanceof SubClass; // truemyObject instanceof BaseClass; // truemyObject;// get// SubClass// BaseClass: 3
Multiple inheritance with implements
var ActiveBuffer = ; var buffer = ;bufferclear; buffer instanceof Buffer; // truebuffer instanceof EventEmitter; // falsebuffer instanceof Clearable; // false
Static members
var Singleton = ; Singletoninstance;
Type Information
The keyword instanceof
can be used to check the type inherited directly. But it doesn't work with implemented
types.
Use Class.is
for all the cases:
Class
We also have two aliases:
ClassClass
License
MIT/X11 License