473,395 Members | 2,222 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,395 software developers and data experts.

Getters and Setters For Object Properties in JS 1.5 and ECMAScript

Hi,
In JavaScript 1.5, objects can use special getter and setter functions
[1] for properties. However, these only seem to be implemented in Gecko
and, AFAICT, don't seem to be part of ECMAScript.

Is there an alternative syntax I can use that is standardised in
ECMAScript and also (preferably) interoperably implemented in several
browsers? Or, do I have to use ordinary getFoo() and setFoo() functions.
Example using JS 1.5 Getters and Setters:
(only works in Gecko)

// Prototype
function something() {
var foobar = "bar"
// logic to validate val argument omitted
this.foo setter = function(val) { return foobar = val; }
this.foo getter = function() { return foobar; }
}

// Create instance
var thing = new something();

// Use properties
alert("Before: foo = " + thing.foo);
thing.foo = "baz";
alert("After: foo = " + thing.foo);
Example using getFoo() and setFoo() functions:
(works in everything tested, including IE, Opera and Firefox)

// Prototype
function something() {
var foo = "bar"
// logic to validate val argument omitted
this.setFoo = function setFoo(val) { foo = val; };
this.getFoo = function() { return foo; };
}

// Create instance
var thing = new something();

// Use properties
alert("Before: foo = " + thing.getFoo());
thing.setFoo("baz")
alert("After: foo = " + thing.getFoo());

[1]
http://www.jalix.org/ressources/inte...j.html#1018325

--
Lachlan Hunt
http://lachy.id.au/
http://GetFirefox.com/ Rediscover the Web
http://GetThunderbird.com/ Reclaim your Inbox
Jul 23 '05 #1
2 2746


Lachlan Hunt wrote:

In JavaScript 1.5, objects can use special getter and setter functions
[1] for properties. However, these only seem to be implemented in Gecko

Getter/setters are implemented in Spidermonkey, the JavaScript engine
Mozilla browsers use, but that engine is also used in other
applications. Gecko is the rendering engine Mozilla browsers uses, like
Spidermonkey it is a part of the browsers. But Gecko doesn't implement
getter and setters in the JavaScript language, that is something the
JavaScript engine Spidermonkey does.
There is also the Rhino engine implementing JavaScript in Java, it
doesn't support getters/setters.
and, AFAICT, don't seem to be part of ECMAScript.
That is right, ECMAScript edition 1, 2, 3 don't have getters/setters, I
am not sure about the planned ECMAScript edition 4.
Is there an alternative syntax I can use that is standardised in
ECMAScript and also (preferably) interoperably implemented in several
browsers? Or, do I have to use ordinary getFoo() and setFoo() functions.
You have to use ordinary getFoo/setFoo functions.

Example using JS 1.5 Getters and Setters:
(only works in Gecko)

// Prototype
function something() {
var foobar = "bar"
// logic to validate val argument omitted
this.foo setter = function(val) { return foobar = val; }
this.foo getter = function() { return foobar; }


Note that that syntax is already deprecated, to keep the syntax of
ECMAScript edition 3 unchanged and to not collide with ECMAScript
edition 4 plans the only way you should use getter/setter in JavaScript
1.5 is via
this.__defineGetter__('foo', function () { return ...; });
this.__defineSetter__('foo', function (val) { return ...; });
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote:
Getter/setters are implemented in Spidermonkey, the JavaScript engine
Mozilla browsers use, but that engine is also used in other
applications. Gecko is the rendering engine Mozilla browsers uses...


Thank you, I've always been confused by all the different names Mozilla
uses for all its components. Now I know a little better :-)
and, AFAICT, don't seem to be part of ECMAScript.


That is right, ECMAScript edition 1, 2, 3 don't have getters/setters, I
am not sure about the planned ECMAScript edition 4.


A search for "ECMA getters and setters" led me to find this earlier
http://www.mozilla.org/js/language/e...rs-and-setters
Is there an alternative syntax I can use that is standardised in
ECMAScript and also (preferably) interoperably implemented in several
browsers? Or, do I have to use ordinary getFoo() and setFoo() functions.


You have to use ordinary getFoo/setFoo functions.


Ok, will do.
this.foo setter = function(val) { return foobar = val; }
this.foo getter = function() { return foobar; }


Note that that syntax is already deprecated, to keep the syntax of
ECMAScript edition 3 unchanged and to not collide with ECMAScript
edition 4 plans the only way you should use getter/setter in JavaScript
1.5 is via
this.__defineGetter__('foo', function () { return ...; });
this.__defineSetter__('foo', function (val) { return ...; });


I couldn't find any good documentation for that syntax, but if they're
not standardised either and only work in Spidermonkey, I'll stick with
the get and set functions.

Thanks for your help!

--
Lachlan Hunt
http://lachy.id.au/
http://GetFirefox.com/ Rediscover the Web
http://GetThunderbird.com/ Reclaim your Inbox
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&...
2
by: Wei Wang | last post by:
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...
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
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...
3
by: planey | last post by:
Hi, I was reading topic about setters and getters (http://www.thescripts.com/forum/thread631267.html) and wanted to ask, how they should be used? I have noticed that a general data validators suck,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.