Last active
October 5, 2016 20:05
-
-
Save edoardocavazza/b81a98a2d84db50e610dfe52c60be739 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
const MAP = (typeof self.WeakMap !== 'undefined') ? new WeakMap() : false; | |
const PROP = '__private__'; | |
function internal(object) { | |
if (MAP) { | |
if (!MAP.has(object)) { | |
MAP.set(object, {}); | |
} | |
return MAP.get(object); | |
} | |
if (!this.hasOwnProperty(internal.fallbackKey)) { | |
this[internal.fallbackKey] = {}; | |
} | |
return this[internal.fallbackKey]; | |
} | |
internal.fallbackKey = PROP; | |
internal.destroy = function(object) { | |
if (MAP) { | |
if (MAP.has(object)) { | |
MAP.delete(object); | |
} | |
} else { | |
delete this[PROP]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo