getter and setter inheritance

Kris Zyp kris at sitepen.com
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 A.foo, 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 A.foo 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}

A.bar -> undefined
A.foo = 'new value' -> setter is not called, foo is not set

Thanks,
Kris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20080509/07b70bdc/attachment-0002.html 


More information about the Es4-discuss mailing list