accessing/modifiying getter and setter functions
On Apr 1, 2007, at 7:15 PM, Kris Zyp wrote:
According to the spec, it is possible to define getters and setters
for the properties. I am wondering if it is possible to access and
modify the getter and setter for a property. Can I dynamically set
the getter for an object?:obj.get x = function() { return 10; }
Can I access the getter function/method?:
myGetter = obj.get x;
Or use the same getter for multiple properties?:
obj.get y = obj.get x;
The syntax wouldn't look like that (too error-like, prone to
automatic semi-colon insertion), but something like SpiderMonkey's
getter= and setter=, which are also available as define
{Getter,Setter} via Object.prototype could be done. SpiderMonkey as
lookup{G,S}etter too, as you requested ;-). But these are not
proposed for ES4, they raise a few questions:
- Under what conditions should it not be possible to replace an
existing property via defineGetter or defineSetter. - What's the best syntax, given that the old getter/setter= (and o =
{p getter: function(){...}} etc.) pattern died around the time of ES3? - Any other gotchas?
At this point we are deferring further work to a post-ES4 edition. I
expect any proposal along the lines of {define,lookup}{G,S}etter
would be deferred too. It's conceivable that other implementations
might copy SpiderMonkey's Object.prototype.__... methods in order to
encourage standardization in the next ECMA edition.
Yes, I am aware the spidermonkey methods. However, IE has failed to take Spidermonkey's lead yet on implementing these, I just was hoping ES4 would mandate something like (or some equivalent) define{G,S}etter and proto, so that IE would someday provide these as well. In particular it seems very unfortunate that there is no standard way to determine the prototype of an object (proto). Sorry I am a little late with these comments... Kris
According to the spec, it is possible to define getters and setters for the properties. I am wondering if it is possible to access and modify the getter and setter for a property. Can I dynamically set the getter for an object?:
obj.get x = function() { return 10; }
Can I access the getter function/method?:
myGetter = obj.get x;
Or use the same getter for multiple properties?:
obj.get y = obj.get x;
Thanks, Kris