Created
June 10, 2015 19:28
-
-
Save FremyCompany/55f5f6fc44f8e62afa2a to your computer and use it in GitHub Desktop.
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
| // | |
| // HELPER | |
| // | |
| var MetadataHolder = function() { | |
| var map = new WeakMap(); | |
| var get = function(obj) { | |
| return map.get(obj) | |
| } | |
| get.init = function(obj, val) { | |
| map.set(obj, val); | |
| } | |
| return get; | |
| } | |
| // | |
| // USAGE | |
| // | |
| var GateKeeper = function() { | |
| var get = MetadataHolder(); | |
| function GateKeeper(secret) { | |
| get.init(this, { secret: secret }); | |
| } | |
| GateKeeper.prototype.isSecretEqualTo = function(claimedSecret) { | |
| return get(this).secret === claimedSecret; | |
| } | |
| return GateKeeper; | |
| }(); | |
| var s = {}; | |
| var o = new GateKeeper(s); | |
| o.isSecretEqualTo(s); // true | |
| o.isSecretEqualTo({}); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment