OvyJS
A fast, elegant, powerful, and cross platform JavaScript OOP library. Support statics, singleton, super call, private, mixins, inherits and more.
Features
- Work on both server and client side.
- Support CommonJS.
- Support statics, singleton, super call, private, mixins, inherits.
Setup
OvyJS supports both server side (CommonJS) and client side JavaScript (browser).
Browser:
API
Class Declaration
There is just one single method you need to remember for class creation: Ovy.define
. Its basic syntax is as follows:
Ovy;
- members is an object represents a collection of class members in key-value pairs
Example:
var Person = Ovy; var aaron = 'Aaron'; aaron; // alert("Salad");
Class extention
var Developer = Ovy;
Mixins
var CanSing = Ovy;var Musician = Ovy
In this case the Musician class will get a sing method from CanSing mixin.
But what if the Musician already has a sing method? Or you want to mix in two classes, both of which define sing? In such a cases it's good to define mixins as an object, where you assign a name to each mixin:
var Musician = Ovy
In this case the sing method of Musician will overwrite the mixed in sing method. But you can access the original mixed in method through special mixins property.
Inherits
Like mixins
, but inherits
just copy all parent's own properties and methods to sub class, inhertis
has higher performance than mixins
but less features than mixins
.
var Musician = Ovy
Singleton
var Logger = Ovy; Logger;
When singleton
config set to true, the class will be instantiated as singleton
Statics
Static members can be defined using the statics
config
var Computer = Ovy; var dellComputer = Computer;var appleComputer = Computer; ; // using the auto-generated getter to get the value of a config property. Alerts "Mac" ; // Alerts "2"
Private properties
In some cases, it is helpful to create a nested scope to contain some private properties. The best way to do this is to pass a function instead of an object as the second parameter. This function will be called to produce the class body:
var Bar = Ovy;
When using this form of Ovy.define, the function is passed tow references to its parent class and parent prototype. This can be used as an efficient way to access any parent class and parent prototype properties and functions:
var Bar = Ovy;