04.28.05
Javascript properties | accessors | getter / setter
Getter and setter functions are nice for data hiding, and even nicer when you can use them invisibly like plain data.
//they can be declared on an object literal
var o = {_name : "Start name",
writes : 0,
reads : 0,
name getter : function () {this.reads++; return this._name;},
name setter : function (n) {this.writes++; return this._name = n;}
}
//and can be added to extant objects
o.numwrites getter = function() { return this.writes;}
o.numwrites setter = function() {throw "no";}
//then use them
o.name ="adsfasdfadsF";
var foo = o.name;
We’ve been trying to find this syntax for a long time, however it is very difficult to search for without knowing the keywords. The keywords ‘get’ and ’set’ will work when declaring an object literal but not when adding it to an already extant object.
Thank you http://www.webfx.nu/dhtml/mozInnerHTML/mozInnerHtml.html