どうやったら効率的なものを書けるだろうか
Dictionary = function() {
var self = function(k, v) {
var state = self.state;
if (v === undefined) {
return state(k);
}
else {
self.state = state(k) === v ? state : function(a) { return a === k ? v : state(a) }
return v;
}
};
self.state = function(k) { return undefined };
return self;
};
var dict = new Dictionary;
dict(1, 2)
dict('1', 3)
var f = function() { hoge };
var g = function() { fuga };
dict(f, 4)
dict(g, 5)
console.log(dict (1));
console.log(dict ('1'));
console.log(dict (f));
console.log(dict (g));