Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Mapping {
// Subclasses define at least @@iterator().
@@iterator() {
throw TypeError("abstract operation")
}
// Mutable subclasses also define set() and delete().
set(key, value) {
throw TypeError("mapping is not mutable");
}
delete(key) {
throw TypeError("mapping is not mutable");
}
// The other methods are defined in terms of the basic operations above.
// But typically a subclass will also define at least get(), has(), and size, for performance.
get size() {
let n = 0;
for (let _ of this)
n++;
return n;
}
get(key) {
for (let [k, v] of this)
if (sameValue(k, key))
return v;
}
has(key) {
for (let [k, v] of this)
if (sameValue(k, key))
return true;
return false;
}
clear() {
for (let k of [...this.keys()])
this.delete(k);
}
*entries() {
for (let pair of this)
yield pair;
}
*keys() {
for (let [k, _] of this)
yield k;
}
*values() {
for (let [_, v] of this)
yield v;
}
forEach(callbackFn, thisArg) {
// This would be specified in terms of [[Call]] rather than the .call method.
for (let [k, v] of this)
callbackFn.call(thisArg, v, k, this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.