Created
July 2, 2014 03:24
-
-
Save akotlov/62c448c9e641df644888 to your computer and use it in GitHub Desktop.
Public,private,static variables in JavaScript
This file contains 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
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