Created
October 18, 2019 19:35
-
-
Save jridgewell/db3bedc4b626f1fb69e45006fa478161 to your computer and use it in GitHub Desktop.
WeakMap implemented via Chrome's internal PrivateSymbol
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
| class WeakMap { | |
| #WeakMapData = %CreatePrivateSymbol('[[WeakMapData]]'); | |
| delete(key) { | |
| const sym = this.#WeakMapData; | |
| if (!%HasProperty(key, sym)) { | |
| return false; | |
| } | |
| delete key[sym]; | |
| return true; | |
| } | |
| get(key) { | |
| return key[this.#WeakMapData]; | |
| } | |
| has(key) { | |
| return %HasProperty(key, this.#WeakMapData); | |
| } | |
| set(key, value) { | |
| const sym = this.#WeakMapData; | |
| if (typeof key !== 'object') { | |
| throw new TypeError('nope'); | |
| } | |
| key[sym] = value; | |
| return this; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment