Is it possible to implement dynamic getters/setters in JavaScript?
问题 I am aware of how to create getters and setters for properties whose names one already knows, by doing something like this: // A trivial example: function MyObject(val){ this.count = 0; this.value = val; } MyObject.prototype = { get value(){ return this.count < 2 ? \"Go away\" : this._value; }, set value(val){ this._value = val + (++this.count); } }; var a = new MyObject(\'foo\'); alert(a.value); // --> \"Go away\" a.value = \'bar\'; alert(a.value); // --> \"bar2\" Now, my question is, is it