I wrote the following:
Object.prototype.length = function(){
var count = -1;
for(var i in this) count++;
return count;
}
It works. But when I execute my page, even without using this function, Firebug tells me that jQuery’s .appendTo() is no longer a function. Why would this be?
That prototype extension is breaking the
$.eachmethod, because this method detects between arrays and objects using thelengthproperty (in jQuery 1.4.2):As you can see, the
isObjvariable will be true only if it doesn’t contains alengthproperty (or the property value isundefined).If
isObjis false, jQuery will try to iterate using a normalforloop:Then, the
appendTomethod is created using$.each, that’s why is not defined:I will always recommend to stay away from extending
Object.prototype, when you extend this prototype ALL objects receive those additional properties.This is especially problematic since when you iterate over the properties of the object
these new properties appear, causing all sorts of unexpected behavior.