Rhino Prompt…
js> a[0] = 1;
js> a.print = function () {for(var e in this) print ("a[" + e + "] = " + a[e]);};
function () {
for (var e in this) {
print("a[" + e + "] = " + a[e]);
}
}
js> a.print();
a[0] = 1
a[print] =
function () {
for (var e in this) {
print("a[" + e + "] = " + a[e]);
}
}


Beyond demonstrating how you can hook a function into an array in javascript, the snippet shows that “this” is very powerful keyword in dynamic languages. While in static languages, “this” often only serves for clarity.
2009.02.17 @ 5:40 pm