Last active
August 29, 2015 14:17
-
-
Save romabelka/642dd8a68c58c008bbc6 to your computer and use it in GitHub Desktop.
Private object attributes using ES6 WeakMap
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 My = (function() { | |
var privates = new WeakMap; | |
return function(a) { | |
privates.set(this, a); | |
this.setter = function(a) { | |
privates.set(this, a) | |
}; | |
this.getter = function() { | |
return privates.get(this) | |
}; | |
} | |
})() | |
my = new My(1) | |
console.log(my.getter()); | |
my.setter(2) | |
console.log(my.getter()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment