Skip to content

Instantly share code, notes, and snippets.

@tenorok
Created November 15, 2013 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenorok/7484597 to your computer and use it in GitHub Desktop.
Save tenorok/7484597 to your computer and use it in GitHub Desktop.
JavaScript: Private properties
/* 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