Last active
October 29, 2015 13:29
-
-
Save Pajk/3fee0119d87dac1bfdf1 to your computer and use it in GitHub Desktop.
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
"use strict" | |
var contexts = new WeakMap() | |
var private_for = function(key) { | |
var ctx = contexts.get(key) | |
if (!ctx) { | |
ctx = {} | |
contexts.set(key, ctx) | |
} | |
return ctx | |
} | |
var helperFactory2 = function() { | |
var proto = {} | |
proto.getHiddenProperty = function() { | |
return private_for(this).hidden_property | |
} | |
proto.changeHiddenProperty = function(new_val) { | |
private_for(this).hidden_property = new_val | |
} | |
var factory = {} | |
factory.createHelper = function() { | |
var obj = Object.create(proto) | |
// set defaults | |
private_for(obj).hidden_property = "hello" | |
return obj | |
} | |
return factory | |
} | |
var factory = helperFactory2() | |
var helper1 = factory.createHelper() | |
var helper2 = factory.createHelper() | |
console.log(helper1.getHiddenProperty() === helper2.getHiddenProperty()) | |
console.log("helper1", helper1.getHiddenProperty()) | |
console.log("helper2", helper2.getHiddenProperty()) | |
helper1.changeHiddenProperty("hi") | |
helper2.changeHiddenProperty("ciao") | |
console.log(helper1.getHiddenProperty() === helper2.getHiddenProperty()) | |
console.log("helper1", helper1.getHiddenProperty()) | |
console.log("helper2", helper2.getHiddenProperty()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please do not use this approach:).