I hadn't seen a problem with Prototype's Object.prototype.extend method
until recently, when I added Xinha support to the Paste
filebrowser
example, and it broke Xinha. I would have actually been mystified
about the breakage, if I hadn't read Bob's
rant on it (and another critique of Object.prototype).
Here's the code in question:
Object.prototype.extend = function(object) {
for (property in object) {
this[property] = object[property];
}
return this;
}
Here's what I first replaced it with (and this didn't work!):
function extend(ob1, ob2) {
for (property in ob2) {
ob1[property] = ob2[property];
}
return ob1;
}
The problem was that methods became unbound, and Prototype uses
.extend({...}) to do something subclass-like. I'm personally
highly confused about how functions become bound to objects. In this
case, this would be set to window instead of ob1 (window is the browser's
global object and default this). But this version worked:
function extend(ob1, ob2) {
return (function(object) {
for (property in object) {
this[property] = object[property];
}
return this;
}).apply(ob1, [ob2]);
}
And I modified prototype
to make use of the function. I don't know if I'll continue to use Prototype or not -- there's
an explosion of Javascript libraries, and it's very hard to choose. Some people's priorities are
different than mine (for instance, comparison hasn't come up for me as an issue, and Dojo's <http://dojotoolkit.org/> obsession with transports doesn't make sense to me either,
and anything with server-side integration is a complete non-starter for me). I'll keep experimenting.
Explanations or pointers to exactly how and when this gets bound would be of
interest to me, if anyone has seen them. It seems to be highly contextual -- there's nothing
like Python's BoundMethod.