getter and setter inheritance
Erik Arvidsson
erik.arvidsson at gmail.com
Mon May 12 10:10:30 PDT 2008
How does super lookups and calls work for getters and setters in ES3.1
and ES4? It seems like the following scheme should work.
Given the following code in ES4:
class A {
function get x() {
...
}
function set x(value) {
...
}
}
class B extends A {
private let _x = 0;
function get x() {
return super.x;
}
function set x(value) {
_x = value;
super.x = value;
}
}
In JS1.7 I think the following works
function A() {
...
}
A.prototype.__defineGetter__('x', ...);
A.prototype.__defineSetter__('x', ...);
function B() {
A.call(this);
}
B.prototype = new A; // Or use helper to prevent calling A's
constructor while setting up the prototype object
B.prototype._x = 0;
B.prototype.__defineGetter__('x', function() {
var aGetter = A.prototype.__lookupGetter__('x);
return aGetter.call(this);
};
B.prototype.__defineSetter__('x', function(value) {
this.x_ = value;
var aSetter = A.prototype.__lookupSetter__('x);
aSetter.call(this, value);
};
Would ES3.1 work by simply replacing __lookup(G|S)etter__ by Object.getProperty?
--
erik
On Mon, May 12, 2008 at 08:52, Lars Hansen <lhansen at adobe.com> wrote:
> The generated getter and setter ought to have roughly the same
> behavior as the getting and setting behavior for a property on
> the class.
>
> The behavior for reading and writing properties is this, IIRC:
>
> If the class is compiled in standard mode, then reads from nonexistent
> properties return undefined. Writes to nonexistent properties
> on non-dynamic classes fail silently, whereas writes to nonexistent
> properties on dynamic classes create those properties.
>
> If the class is compiled in strict mode, then reads from nonexistent
> properties on non-dynamic classes throw an exception (no such property);
> reads from nonexistent properties on dynamic classes return undefined.
> Writes to nonexistent properties on non-dynamic classes throw an
> exception (no such property), whereas writes to nonexistent properties
> on dynamic classes create those properties.
>
> For generated getters and setters (in classes at least), it seems
> that matching that behavior, except for property creation, is
> roughly right. I'd say that the write ought to be silently ignored
> on non-strict classes and that an error should be thrown on strict
> classes.
>
> And yes, it ought to be possible to invoke the superclass getter,
> the syntax is as for accessing a property on a superclass,
> "super.p" for the immediate base class or "super(C).p" for classes
> higher up the inheritance chain.
>
> --lars
>
>
> > -----Original Message-----
> > From: P T Withington [mailto:ptwithy at gmail.com] On Behalf Of
> > P T Withington
> > Sent: 12. mai 2008 07:59
> > To: Lars Hansen
> > Cc: Kris Zyp; es3.x-discuss at mozilla.org; es4-discuss Discuss
> > Subject: Re: getter and setter inheritance
> >
>
>
> > On 2008-05-09, at 12:46 EDT, Lars Hansen wrote:
> >
> > > (One bike ride and one cup of coffee later.)
> > >
> > > Clearly there is a difference between class/interface
> > inheritance on
> > > the one hand and prototype inheritance on the other.
> > >
> > > In either case I think the introduction of a setter and/or
> > a getter in
> > > a context introduces definitions for both in that context,
> > essentially
> > > a special property that holds a getter/setter pair. A missing
> > > getter/setter is generated (that's what ES4 specifies now.) That
> > > means that in prototype contexts, if an object has a getter or a
> > > setter for a
> > > field, the prototype will never be searched for the missing half.
> > > In a
> > > class context, getters and setters can be overridden
> > because the class
> > > instance only has the one special property with the getter/setter
> > > pair, and the values in that property depend on the class that the
> > > instance is an instance of. So different classes have different
> > > pairs.
> >
> > (I've only been to spin class, but I've had 1 coffee and 2 teas.)
> >
> > When "A missing getter/setter is generated", what is its
> > functionality? Does it just error?
> >
> > Can I call a super getter/setter method (I hope)? What is
> > the syntax for that?
> >
> >
> _______________________________________________
> Es3.x-discuss mailing list
> Es3.x-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es3.x-discuss
More information about the Es4-discuss
mailing list