Main Page

Implementations

Implementations
Although ECMAScript Edition 4 isn’t an officially released standard, that hasn’t stopped both Microsoft
and Mozilla from implementing some of it.
Mozilla
Mozilla chose to implement some small features in the Web browser. For instance, it is possible to define
constant values in Mozilla using the
const
keyword:
const message = “Hello World!”;
This line causes an error in all other browsers because
const
is a reserved word in ECMAScript Edition 3.
In Mozilla, however, this line defines a constant string named
message
that cannot have its value changed.
Mozilla also chose to implement getters and setters for object properties, albeit with a different syntax.
You may remember getters and setters from earlier chapters of this book, but here is a brief example:
function MyClass() {
this.__color__ = “red”;
}
MyClass.prototype.color getter = function () {
return this.__color__;
};
MyClass.prototype.color setter = function (value) {
this.__color__ = value;
alert(“Color changed to “ + value);
};
var obj = new MyClass();
alert(obj.color); //outputs “red”
obj.color = “blue”; //outputs “Color change to blue”
This is an alternate syntax (so as not to break syntax in non-Mozilla browsers):
function MyClass() {
this.__color__ = “red”;
}
MyClass.prototype.__defineGetter__(“color”, function () {
return this.__color__;
});
MyClass.prototype.__defineSetter__(“color”, function (value) {
this.__color__ = value;
alert(“Color changed to “ + value);
});
var obj = new MyClass();
alert(obj.color); //outputs “red”
obj.color = “blue”; //outputs “Color change to blue”
604
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 604


JavaScript EditorFree JavaScript Editor     Ajax Editor


©