Skip to content

Instantly share code, notes, and snippets.

@andreazevedo
Created March 12, 2013 15:19
Show Gist options
  • Save andreazevedo/5143784 to your computer and use it in GitHub Desktop.
Save andreazevedo/5143784 to your computer and use it in GitHub Desktop.
Good pattern for defining javascript classes.
/** Class Foo **/
var Foo = (function () {
// Private variables
var private_number = 200
/** class Constructor **/
var Foo = function () {
this.bar = 0;
};
/** class methods **/
Foo.prototype = {
add: function () {
this.bar += private_number;
}
};
return Foo;
})();
foo = new Foo();
alert(foo.bar); // 0
foo.add();
alert(foo.bar);// 200
alert(foo.private_number); //undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment