See the code example below how to define a Javascript Class with private, public and static members (according this video). This format is used while using the class.js to make classic Class inheritance possible for your Javascript projects.
// self executing function, so this Class has it's own scope ( function() { // public static member MyClass.STATIC_MEMBER = "value"; // private member var _privateMember; // constructor function MyClass( value ) { _privateMember = value; // public getter this.getPrivateMember = function() { return _privateMember; }; // public setter this.setPrivateMember = function( value ) { _privateMember = value; }; }; // expose this Class for public usage window.MyClass = Class.extend( MyClass ); // public member MyClass.prototype.publicMember = "value"; // public method MyClass.prototype.publicMethod = function() { privateMethod( this ); }; // private method function privateMethod( context ) { console.log( context.publicMethod ); }; })( window );
Make sure you define your prototype members and methods after you called Class.extend!
If you have overridden a public method, you can still access the method in it's subclass. This can be done by calling the this.parent method.
( function() { function MySubclass() {}; window.MySubclass = Class.extend( MySubclass ); MySubclass.prototype.say = function( value ) { console.log( value ); }; })( window );
( function() { function MyExtendedClass() {}; window.MyExtendedClass = MySubclass.extend( MyExtendedClass ); MyExtendedClass.prototype.say = function( value ) { this.parent( "I'm saying " + value ); }; })( window );