467,876 Members | 1,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,876 developers. It's quick & easy.

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
  • viewed: 2482
Share:
2 Replies


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by kelvSYC | last post: by
2 posts views Thread by Wei Wang | last post: by
112 posts views Thread by mystilleef | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.