Created
November 26, 2016 08:13
-
-
Save skylying/fdfd8afd5cd49ed9c3a1df5c24ca227c to your computer and use it in GitHub Desktop.
Private variable 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
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