Created
November 15, 2013 13:51
-
-
Save tenorok/7484597 to your computer and use it in GitHub Desktop.
JavaScript: Private properties
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
/* Constructor */ | |
function Klass(text, separator) { | |
this.text = text; | |
this.separator = separator; | |
/* Private properties */ | |
var _privateField = 'World'; | |
var _privateMethod = function() { | |
return this.separator + _privateField + Klass.staticMethod() + _privateStaticMethod(); | |
}.bind(this); | |
/* Private static properties */ | |
var _privateStaticMethod = function() { | |
return ' =)'; | |
}; | |
/* Public properties */ | |
this.publicMethod = function() { | |
return this.text + _privateMethod(); | |
}; | |
} | |
/* Public static properties */ | |
Klass.staticMethod = function() { | |
return '!'; | |
}; | |
console.log(new Klass('Hello', ', ').publicMethod()); // Hello, World! =) | |
console.log(Klass.staticMethod()); // ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment