Features
- Small footprint, no dependency, 0.7K minimized+gzip!
- Super fast! See benchmark.
- Work on both server and client side.
- Support CommonJS.
- Support getter/setter, constant, main, singleton, mixin, private properties, Aspect Oriented Programming.
- Plugins mechanism to extend itself.
Setup
JSFace supports both server side (CommonJS) and client side JavaScript (browser).
Browser
bower
bower install jsface
dnsjs
<script src="https://cdn.jsdelivr.net/jsface/2.4.8/jsface.min.js"></script>
Manually
NodeJS environment
First install JSFace via npm:
npm install jsface
Then use its APIs, for example:
var jsface =Class = jsfaceClassextend = jsfaceextend;
Usage
Define a class
var Person =;var person = "Rika" 20;person; // "Rika/20"
Define a sub-class
var Student =;var student = 1 "Rika" 20;student; // "1/Rika/20"
main
JSFace supports a special method named main(). main() is executed right after the class is created.
;
Singleton class
var Util =;Util; // 2012
Static properties
var Person =;PersonMIN_AGE === 1; // truePersonMAX_AGE === 150; // truePerson; // false
Constants
Constants work the same as static properties. The only different is they are immutable.
var Person =;PersonMIN_AGE = -1;PersonMIN_AGE === 1; // true, MIN_AGE is immutable
Private properties
JSFace supports private static properties, meaning the properties are shared over instances.
var Person =;
Mixins
JSFace provides a powerful mechanism to support mixins. Reusable code can be mixed into almost anything.
Mixin can be bound when you define classes:
var Options =;var Events =;var Person =;// Student inherits Person and extends properties from Options and Eventsvar Student =;var student = 1 "Rika" 20;student; // student.opts === { foo: true }student; // truestudent; // false
Or after defining classes:
var Student =
Mixin with instance:
var person = "Rika" 20;;person;
Mixin with native classes:
;" Hello World "; // "Hello World"
No conflict
In browser environment, you might be using another library which also introduces the global namespace Class. JSFace can return the original Class back to the library claims it with a call to jsface.noConflict().
jsface;// Code that uses other library's Class can follow here// ...// Define classes by using jsface.Class directlyvar Person = jsface;
Plugins
Plug and Play pointcut
JSFace supports Aspect Oriented Programming (AOP) via simple before/after mechanism. You can apply pointcuts over class constructors, class methods, singleton methods, instance methods. You can even apply pointcuts over native classes.
AOP support is implemented as a standalone plugin.
Setup
Browser:
then in your code, make an alias to jsface.pointcut:
var pointcut = jsfacepointcut;
NodeJS:
var pointcut = ;
Applying pointcuts
In JSFace, an advisor is a set of pointcuts you want to apply to a subject. You can apply as many advisors as you want.
Person =;var advisor ={thisage = 20;}{thisemail = "rika@sample.com";}{ // sugar syntax, foo:beforethiscounter++;}bar:{thiscounter++;}{thiscounter++;}};Person = ;var person = "Rika";person;person;personname === "Rika"; // truepersonage === 20; // truepersonemail === "rika@sample.com"; // truepersoncounter === 3; // true
Removing pointcuts
Using previous apply pointcut example:
// remove all pointcuts bound to constructor and fooPerson = ;// remove advisor, other advisors remainedPerson = ;// remove all advisors, restore the fresh version of PersonPerson = ;
$ready
$ready plugin is designed to help a parent class to intercept their subclasses' creation.
Sample
var Service =;var SessionService =;var ApplicationService =;
Bug tracker
Have a bug? Please create an issue here on GitHub!
License
Copyright (c) Tan Nhu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.