Created
September 20, 2016 02:02
-
-
Save kuroisuna/67d98f8a463c2bb1476c63fc8feb6c88 to your computer and use it in GitHub Desktop.
JS: Simple OOP private/public methods and 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
var helper = function (element) { | |
/** | |
* [private] Base HTML div | |
* @type {HTMLDivElement} | |
*/ | |
var element = element; | |
/** | |
* [public] Count of base element childrens | |
* @type {Number} | |
*/ | |
this.count = element.children.length; | |
/** | |
* [private] Returns a random token | |
* @return {string} | |
*/ | |
var encrypt = function () { | |
return Date() | |
.toString() | |
.replace(/\W+/g, '') | |
.toLowerCase(); | |
}; | |
/** | |
* [public] Executes the private "encrypt" method | |
* @return {string} A random token | |
*/ | |
this.e = function () { | |
return encrypt(); | |
}; | |
}; | |
// Usage: | |
var mainElement = document.querySelector("#main"); | |
var subElement = document.querySelector("#sub"); | |
var mainHelper = new helper(mainElement); | |
// -> helper {count: 12} | |
var subHelper = new helper(subElement); | |
// -> helper {count: 4} | |
mainHelper.e(); | |
// -> "monsep192016205438gmt0500cot" | |
subElement.e(); | |
// -> "monsep192016205847gmt0500cot" | |
mainHelper.encrypt(); | |
// -> Uncaught TypeError: a.encrypt is not a function(…) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment