Created
January 29, 2014 10:34
-
-
Save leeroybrun/8685383 to your computer and use it in GitHub Desktop.
Object-Oriented Javascript - Class - Methods - Static - Private - Public
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From : http://stackoverflow.com/a/1635143/1160800 | |
// constructor function | |
function MyClass () { | |
var privateVariable; // private member only available within the constructor fn | |
this.privilegedMethod = function () { // it can access private members | |
//.. | |
}; | |
} | |
// A 'static method', it's just like a normal function | |
// it has no relation with any 'MyClass' object instance | |
MyClass.staticMethod = function () {}; | |
MyClass.prototype.publicMethod = function () { | |
// the 'this' keyword refers to the object instance | |
// you can access only 'privileged' and 'public' members | |
}; | |
var myObj = new MyClass(); // new object instance | |
myObj.publicMethod(); | |
MyClass.staticMethod(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment