WeakPair primitive ?
Sean Eagan
seaneagan1 at gmail.com
Thu Sep 15 10:21:46 PDT 2011
Would a WeakPair primitive be useful...
let wp = new WeakPair(key, value);
...
if(wp.hasKey(key)) {
foo(wp.value());
}
WeakMap could be built on top of this primitive:
class WeakMap {
constructor() {
private pairs = new Set;
private getPair = function(key) {
for(i of private(this).pairs.iterator()) {
if(i.hasKey(key)) return i;
}
return undefined;
}
}
get(key) {
let pair = private(this).getPair(key);
return pair ? pair.value() : undefined;
}
has(key) {
return !!private(this).getPair(key);
}
set(key, val) {
if(!isObject(key)) throw new Error;
let pair = private(this).getPair(key);
let pairs = private(this).pairs;
if(pair) {
pairs.delete(pair);
}
pairs.add(new WeakPair(key, value));
}
delete(key) {
let pair = private(this).getPair(key);
if(pair) {
let pairs = private(this).pairs;
pairs.delete(pair);
}
}
}
Are there other useful abstractions that could be built on top of
WeakPair, beyond WeakMap ?
Thanks,
Sean Eagan
More information about the es-discuss
mailing list