Set your Setter and Get your Getter

Getters and Setters are a lot of fun to use. Unfortunately not all browsers implement them. According to John Resig’s post JavaScript Getters and Setters only two browsers currently support them (Opera 9.5 has since been release so make it three). Even before I was really into Javascript programming I used PHP’s Magic Methods __set and __get.

It is with much happiness that I think I can finally say, without seeming like a fool, that: “JavaScript Getters and Setters are now prevalent enough to become of actual interest to JavaScript developers.” Wow, I’ve been waiting a long time to be able to say that.

John Resig – JavaScript Getters and Setters

As much as I would love this to be true, I still do not quite think we are there. Rhino adding support was good, because with that John was able to implement window.location in env.js

So now that we have some background on setters and getters in Javascript. Here is my implementation for browsers that do not already support it.

Read moreSet your Setter and Get your Getter

histarray – An Experiment with Getters and Setters

While doing an experiment with Getters and Setters I whipped together a small lib for a versioning array, the histarray. So far tested only in Firefox 3, it utilizes __defineGetter__(someNumber, function(){}) to return an element from an internal array (the history). Its push method will either overwrite the current revision if it is not the last, or push a new revision into the history.

A Setter is also defined, so no matter where in the time line you currently are, that element will be updated.

Read morehistarray – An Experiment with Getters and Setters