What I've also tried to epxlain in the very same post, is that Symbol unlocks new patterns for "secret" properties definition, and the following little WeakMap is just one example on how
symbols
can be used to relate data we don't want to show in other common ways.
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 WeakMap = WeakMap || (function (s, dP, hOP) {'use strict'; | |
function WeakMap() { // by Andrea Giammarchi - WTFPL | |
dP(this, s, {value: Symbol('WeakMap')}); | |
} | |
WeakMap.prototype = { | |
'delete': function del(o) { | |
delete o[this[s]]; | |
}, | |
get: function get(o) { | |
return o[this[s]]; | |
}, | |
has: function has(o) { | |
return hOP.call(o, this[s]); | |
}, | |
set: function set(o, v) { | |
dP(o, this[s], {configurable: true, value: v}); | |
} | |
}; | |
return WeakMap; | |
}(Symbol('WeakMap'), Object.defineProperty, {}.hasOwnProperty)); |
Symbol
variables ;-)
No comments:
Post a Comment