petermichaux@gmail.com wrote:
Quote:
Hi,
>
I have searched the archives but didn't find the questions and answers
I am looking for.
>
I have been looking at Prototype.js quite a bit lately as I need to
create a very small library of similar functionality to a subset of
Prototype.js. This is for use with Ruby on Rails.
>
About Prototype.js Rob G wrote:
>
1. It modifies the protoype of some built-in objects so that
using say, for..in with an array object produces unexpected
results.
>
I can see that modifying the prototypes of Object or Array could break
other JavaScript libraries that depend on for..in. Prototype.js also
adds functions to the prototype of Element. How bad is that?
In regard to that particular issue, it is not the modification of the
Object prototype per se that is a problem, but the fact that
prototype.js does it always, regardless of whether it is a good idea or
not for a particular circumstance[1].
[...]
Quote:
Assuming I shouldn't be playing with the prototypes of built-in
JavaScript objects I can see a couple options that might still be
available.
Modifying a built-in object's prototype is not bad by definition, in
fact it is considered OK provided it is done with good reason and only
for a particular circumstance. If you describe a bit more of what you
are actually trying to do, then you may get more specific responses.
[...]
Quote:
Two. I could just be happy with functional programming
>
function updateElement(oElement, html) {
o.element.innerHTML = html;
};
Wherever you put it, it is a function that you want to call somehow.
Whether you add it as a method of some object's prototype, each
instance, an object representing a library, or just a global function
object, really depends on what you are doing and how many of them you
want.
Quote:
What do you guys think or how do you approach this inability to use
built-in objects as prototypes?
There is no inability, just more or less appropriate uses of it. I
generally leave built-in objects alone because I like to know they are
unmodified and there is usually a reasonable work around. Others who
post here regularly will suggest modifying built-in objects on
occasions - e.g. adding a push method to the Array object.
1. Noted that there are efforts to stop prototype.js modifying the
prototypes of built-in objects, but they seem to make things even more
convoluted and require more libraries to be loaded.
--
Rob