You may use ZNOW if you like.
http://icarusso.github.io/ZNOW/private.html
private, protected and public are reserved words. Although ECMASCRIPT 6, the next JavaScript version, support class. It seems not all encapsulation levels are supported. Moreover, the use of it seems a bit verbose and wont be available in any major browsers yet.
http://wiki.ecmascript.org/doku.php?id=harmony:classes
I guess different encapsulation levels can increase code readability and maintainability. Especially when developing complicated programs and collaborating with other programmers. So I designed this framework. :-)
It uses name prefix to declare different encapsulation levels: _ is private and $ is protected.
Example:
var ClassA=Class({
init:function(a){ //constructor
this._a=a;
},
foo:function(){ //public function
return this._a;
},
_a:false //private attribute
})
var a=new ClassA('a');
console.log(a._a == 'a'); //false
console.log(a.foo() == 'a'); //true
This framework allows you to shift your oo-skills from Java to JavaScript without pain.
Another example is Singleton.
var ClassA=Class({
'static.getInstance':function(){
if(!ClassA._instance) ClassA._instance=new ClassA();
return ClassA._instance;
},
'static._instance':false,
_init:function(){ //private constructor
console.log('instance created');
}
})
var a1=ClassA.getInstance(); //>instance created
var a2=ClassA.getInstance();
console.log(a1==a2); //>true
I hope you will find this framework easy to use and easy to read.
ZNOW also supports interface, abstracts and const.
Here you are the link: http://icarusso.github.io/ZNOW/
Hope you enjoy it.
let
is used in newer Mozilla implementations. – Connaught