473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Object.prototype, getters and setters

Greetings,

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.

Thanks for your help.

Regards,
--
Wei
Jul 23 '05 #1
2 3948


Wei Wang wrote:

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.


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/
Jul 23 '05 #2
Martin Honnen wrote:
Wei Wang wrote:
[...]
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.
What you have looks like some pseudo syntax to me,


But it is not entirely. Prototypes of objects inheriting directly
from Object can be defined using an Object literal and getters may
be defined with the `get' keyword before the property identifier:

<http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Def ining_Getters_and_Setters>

Whether the latter really *works* in an implementation is another issue.
It does work in JavaScript 1.5 as implemented in Mozilla/5.0 (tested
successfully with `Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8)
Gecko/20050513 Firefox/1.0.4 (Debian package 1.0.4-1) Mnenhy/0.7.2.0'):

var A = function () {}; // although I'd prefer: function A() {}

A.prototype = {
a: null,
get b() { return this.a++; }
};

var x = new A;
alert(x.b); // 0
alert(x.b); // 1
here is how it works in pratice: [...]


Since the above works in practice as well, I don't see a need to call
the __defineGetter__() method explicitely. Since method calls are
less efficient than variable instantiation, one could probably increase
efficiency with my approach.

What of course does not work is having `a' as identifier for both the
non-function property and the getter; identifiers have to be unique
within a scope.
PointedEars
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Stuart Gerchick | last post by:
C++ Coding Standards : 101 Rules, Guidelines, and Best Practices by Herb Sutter, Andrei Alexandrescu is now a month or so away from release. What is people's opinion on this...is it going to be a...
32
by: kelvSYC | last post by:
I'm familiar with get and set function paradigms from Java, but what's the recommended design for such in C++? Should it be like so: foo& getFoo(); void setFoo(foo& f); or like so: foo&...
8
by: Menon | last post by:
Hi folks I am not at all an SQL server guy (more of an Oracle developer.) I wanted to know if you guys could tell me if SQL Server supports objects in the database. If so, could you kindly point...
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
13
by: Nathan White | last post by:
I was curious if I can make an object method look and act like an object property. example: function Circle(x,y,r){ this.x = x; this.y = y; this.r = r; } Circle.prototype.area = function(){...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
33
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
3
by: =?ISO-8859-15?Q?Arne_Vajh=F8j?= | last post by:
Alain Boss wrote: Believe it or not, but I have actually seen getters and setters being wrong after too much copy paste (it was not C# but that does not really matter). As a consequence I am...
8
by: Hussein B | last post by:
Hey, I noted that Python encourage the usage of: -- obj.prop = data x = obj.prop -- to set/get an object's property value. What if I want to run some logic upon setting/getting a property?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.