js.class
js.class is a class library which focuses on simplifying OOP in javascript by providing rich set of tools like: mixins, inheritance, getters, setters and more.
Version 2.6.0
contains sjs macros for ES6 syntax.
Features:
- super fast!
- small footprint, no dependency, 0.2K minimized
- works on both browser (with UMD wrapper) and node.js
- supports: inheritance, statics, constans, mixins, getters, setters
- typeOf
- build in singleton support
- ES6 syntax via sjs
Installation
Npm
npm install js.class
Bower
bower install js.class
API
Usage
Node.js
var JSClass = require('js.class');
var MyClass = JSClass({
myMethod: function() {} //your method declaration
});
Browser
Normal version
AMD version:
;
JSClass ES6 syntax
JSClass
supports ES6 syntax thanks to sweet.js. In order to use ES6 syntax make sure you have installed sweet.js node loader and library is required as JSClass.
Class declaration
var MyClass = ;
ES6 syntax
{}
Constructor pattern
var MyClass = ; var instance = 12;console;//1console;//2
Getting class
var MyClass = ;var instance = ;console;//true
ES6 syntax
{ thisparam1 = param1; thisparam2 = param2; }
Getters/Setters
Getters/Setters will not work in ie < 8 due to lack of Object.defineProperty support
var MyClass = ;var instance = 12;console;//[1,2]console;//[2] instanceallParams = 34;console;//[3,4]
Check tests for more examples
Inheritance
var MyChildClass = MyClass;
ES6 syntax
{}
Invoking overridden methods
var MyClass = ;var MyChildClass = MyClass;
ES6 syntax
{ } { super;//this will call MyClass#myMethod }
Statics and constans
Constans will not work in ie >=8 due to lack of Object.defineProperty support
Static variables can be easy defined by usage of static
function, which accepts literal object.
var StaticExample = ;console;//myStaticStaticExamplemyStatic = 'otherValue';console;//otherValue
If literal object will contain a key in uppercase js-class will treat a variable as a constans:
var ConstantExample = ;console;//constConstantExampleMY_CONST = 'otherValue';console;//const
Mixins
Mixin is a class which contains a combination of methods from other classes
Its really usefull strategy if you are going to follow DRY methodology.
To define mixin we need to simply use mixin
method:
var Pet = ;var Animal = ;var Dog = ;var pluto = ;pluto;plutoname'pluto'; console;//plutoconsole;//false
ES6 syntax
{ if typeof name === 'undefined' return thisname; thisname = name; } { thisfed = true; } { thisthirsty = false; } implements Pet Animal {} var pluto = ;pluto;plutoname'pluto'; console;//plutoconsole;//false
Singleton
In order to create singleton class set singleton
property to true, eg.:
var Singleton = ; var p1 = Singleton;var p2 = Singleton; p1 === p2;//true ;//will throw an Error
typeOf
js.class provides handy typeOf
method in every instance of class,
the method allows you to determine whather object is a mixin of given class:
var pluto = ; console;//trueconsole;//trueconsole;//trueconsole;//false
ES6 syntax
var pluto = new Dog();
console.log(pluto is Dog);//true
console.log(pluto is Animal);//true
console.log(pluto is Pet);//true
console.log(pluto is MyClass);//false
Instance of support
js.class does support instanceof
operator. Consider the following example:
var MyClass = ;var MyChildClass = MyClass; var t = ; console;//trueconsole;//true
ES6 syntax
class MyClass {
create(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
}
class MyChildClass extends MyClass {
}
var t = new MyChildClass();
console.log(t instanceof MyClass);//true
console.log(t instanceof MyChildClass);//true
For Developers
Running tests
npm install
npm test
Running benchmarks
node ./benchmark/class-declaration.js
node ./benchmark/class-extend.js
node ./benchmark/class-mixins.js
Class declaration benchs
node ./benchmark/class-declaration.js
class x 75,624 ops/sec ±3.23% (86 runs sampled)
js.class x 50,721 ops/sec ±9.67% (63 runs sampled)
klass x 44,743 ops/sec ±8.60% (74 runs sampled)
ee-class x 25,366 ops/sec ±6.20% (77 runs sampled)
Class extension benchs
node ./benchmark/class-extend.js
js.class x 126,312 ops/sec ±3.57% (92 runs sampled)
class x 78,576 ops/sec ±5.05% (85 runs sampled)
klass x 59,602 ops/sec ±7.94% (76 runs sampled)
ee-class x 37,730 ops/sec ±4.70% (85 runs sampled)
Mixins benchs
node ./benchmark/class-mixins.js
js.class x 677,574 ops/sec ±5.74% (86 runs sampled)
class x 541,828 ops/sec ±2.33% (93 runs sampled)
klass x 210,674 ops/sec ±6.61% (83 runs sampled)
ee-class x 140,770 ops/sec ±1.89% (96 runs sampled)
| Note that only js.class supports typeof
method, which allows you to determine whether given object is a mixin of other object/class.
Conclusion
You may notice simple class declaration is the fastest in class
library, but when it comes to more
advanced oop features js.class
is a good choice.
Version History
2.7.0
Added getClass
method
2.6.1
Fixed bug with multiple statics
2.6.0
Added base javascript macros
2.5.3
Added UMD wrapper, organize the build folder and added npm run build
to create both the build and the minified version.
2.5.2
Fix issue with set and get with multiple instances of the same Class (Thanks to fastrde)
2.5.1
Fixed constructor in factored objects
2.5.0
Added setters/getters support
2.4.1
Singleton object cannot be extended
2.4.0
Added singleton support
2.2.6
Mixin method accepts objects as well
2.2.5
Added benchmarks for libraries class
, klass
, ee-class
2.2.1
Fixed instance's statics. Now if you change instance's static it will be changed across all other instances of the same class
2.2.0
Removed behaviour which was copying consts/statics into children class