Skip to content

Instantly share code, notes, and snippets.

@leeroybrun
Created January 29, 2014 10:34
Show Gist options
  • Save leeroybrun/8685383 to your computer and use it in GitHub Desktop.
Save leeroybrun/8685383 to your computer and use it in GitHub Desktop.
Object-Oriented Javascript - Class - Methods - Static - Private - Public
// 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