Skip to content

Instantly share code, notes, and snippets.

@skylying
Created November 26, 2016 08:13
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 skylying/fdfd8afd5cd49ed9c3a1df5c24ca227c to your computer and use it in GitHub Desktop.
Save skylying/fdfd8afd5cd49ed9c3a1df5c24ca227c to your computer and use it in GitHub Desktop.
Private variable in javascript
var SlideshowController = {};
SlideshowController.getPropertyA = function() {
var i = this.instance();
return i.getPropertyA();
}
SlideshowController.getPropertyB = function() {
var i = this.instance();
return i.getPropertyB();
}
SlideshowController.instance = function() {
if (!this._instance) {
this._instance = (function() {
function RealInstance() {
this.propertyA = 'FOO'; // Can only be accessed by getPropertyA()
this.propertyB = 'BAR'; // Can only be accessed by getPropertyB()
}
RealInstance.prototype.getPropertyA = function() {
return this.propertyA;
}
RealInstance.prototype.getPropertyB = function() {
return this.propertyB;
}
var instance = new RealInstance();
return instance;
})()
}
return this._instance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment