getter and setter inheritance

Kris Zyp kris at
Fri May 9 07:18:47 PDT 2008

A question of how inheritance/delegation should work with getters and setters was raised in a recent discussion. If there is an object A whose prototype is object B, and object A defines a getter for property foo, and object B defines a setter for property foo, and we write a value to, should the setter defined on object B be called? If A didn't define any property on A, the setter would be inherited from B and would be called when was modified. However, with the getter defined on A, should the inheritance stop at A, because there is a slot defined, or should it continue along the prototype chain because there was no setter defined for that property (nor plain value)? The question also applies when the getter and setter are reversed. When a property is accessed and there is a setter defined, but no getter, should we continue to down the prototype chain to find a getter or a plain value, or stop and return undefined? AFAICT, ES4 doesn't explicitly define which is the correct behavior. Firefox follows the former behavior:

B={set foo(v){foo = v},
    get bar(){return "bar value"}}
A={get foo(){return "foo value"},
    set bar(v){bar = v},
    __proto__:B} -> undefined = 'new value' -> setter is not called, foo is not set

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Es4-discuss mailing list