Wei Wang wrote:
[color=blue]
> I find the JavaScript's Object.prototype and getter/setter mechanism
> very nice. However, I need some help with extending an object with
> getters/setters in the derived class. For example:
>
> A : function () {}
>
> A.prototype =
> {
> a : null,
>
> get a : function () { return a++; }
> };
>
>
> B : function () {}
>
> B.prototype = new A;
>
> Now, I would like to define a getter/setter in B. How do I do that?
> There is no way to use the same syntax as in "A.prototype = ..." above.[/color]
What you have looks like some pseudo syntax to me, here is how it works
in pratice:
function A () {
this._a = 0;
}
A.prototype.__defineGetter__('a',
function () {
return this._a++;
}
);
function B () {
A.apply(this);
this._b = 0;
}
B.prototype = new A();
B.prototype.__defineGetter__('b',
function () {
return this._b--;
}
);
var b = new B();
alert(b.a);
alert(b.a);
alert(b.b);
alert(b.b);
And note that getters/setters are an extension to the ECMAScript edition
3 standard that is only implemented in the Mozilla JavaScript engine.
--
Martin Honnen
http://JavaScript.FAQTs.com/