By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Getters and Setters For Object Properties in JS 1.5 and ECMAScript

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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.