Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created April 10, 2014 18:05
Show Gist options
  • Save matthewp/10407730 to your computer and use it in GitHub Desktop.
Save matthewp/10407730 to your computer and use it in GitHub Desktop.
WeakMap and private
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);
};
@justinbmeyer
Copy link

Why couldn't I do:

var p = new Public();

privateThings.get(p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment