I have this function declared:
Object.prototype.append = Array.prototype.append = append = function( tag ){
alert( this )
...
}
It supposed to append “tag” element after a DOM element or an array of objects.
Now, when I call it after an array of two elements – everything works well, I get “[object HTMLDivElement,object HTMLDivElement]” thtown by alert for, but when it’s called by a single DOM element IE8 throws that the object does not support this option or method and even the alert is not executed.
A DOM element does not necessarily inherit from the Javascript
Object. You cannot count on that. It might work in some places, but as you have encountered, it does not work everywhere.You can add a method to an existing DOM object after it’s been created, but this can sometimes cause issues. Libraries like prototype that use to extend built-in objects are moving away from that behavior and libraries like jQuery and YUI avoid it entirely.
I would suggest that you are better off using a utility function rather than an add-on DOM method or a wrapper object like jQuery does.