Getters and Setters For Object Properties in JS 1.5 and ECMAScript

L

Lachlan Hunt

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/internet/javascript/_JS15guide/JS15core/obj.html#1018325
 
M

Martin Honnen

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 ...; });
 
L

Lachlan Hunt

Martin said:
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 :)
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/es4/core/functions.html#getters-and-setters
You have to use ordinary getFoo/setFoo functions.

Ok, will do.
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top