Skip to content

Instantly share code, notes, and snippets.

@akotlov
Created July 2, 2014 03:24
Show Gist options
  • Save akotlov/62c448c9e641df644888 to your computer and use it in GitHub Desktop.
Save akotlov/62c448c9e641df644888 to your computer and use it in GitHub Desktop.
Public,private,static variables in JavaScript
function MyClass () { // constructor function
var privateVariable = "foo"; // Private variable
this.publicVariable = "bar"; // Public variable
this.privilegedMethod = function () { // Public Method
alert(privateVariable);
};
}
// Instance method will be available to all instance but only load once in memory
MyClass.prototype.publicMethod = function () {
alert(this.publicVariable);
};
// Static variable shared by all instance
MyClass.staticProperty = "baz";
//...
var myInstance = new MyClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment