Created
April 10, 2014 18:05
-
-
Save matthewp/10407730 to your computer and use it in GitHub Desktop.
WeakMap and private
This file contains hidden or 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 privateThings = new WeakMap(); | |
function Private() { | |
this.data = 'is private'; | |
} | |
Private.prototype.somePrivateFunction = function() { | |
this.foo = 'bar'; | |
}; | |
function Public() { | |
privateThings.set(this, new Private()); | |
} | |
Public.prototype.somePublicFunction = function() { | |
var myPrivate = privateThings.get(this); | |
// myPrivate.data === 'is private'; | |
myPrivate.somePrivateFunction.call(this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why couldn't I do: