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]);
}
}
Comments
One response to “Arrays as Objects in Javascript”
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.