Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Created October 18, 2019 19:35
Show Gist options
  • Select an option

  • Save jridgewell/db3bedc4b626f1fb69e45006fa478161 to your computer and use it in GitHub Desktop.

Select an option

Save jridgewell/db3bedc4b626f1fb69e45006fa478161 to your computer and use it in GitHub Desktop.
WeakMap implemented via Chrome's internal PrivateSymbol
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