alternatives to built-in objects as prototypes

P

petermichaux

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? (I notice
that in Flanagan's JavaScript 4th edition he mentions that you can
modify the prototypes of built in objects however there is no mention
of the dangers involved.)

I then thought I could use an instance of Element as the prototype of a
MyElement constructor but that option has been squashed in the past as
being not cross browser.

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.

One. Could I make a constructor like the following or with the intent I
mean in the following? I imagine this would be slow.

function MyElement(sId){
this.element = document.getElementById(sId);
// expose the properties of this.element as properties of this
// I made this up, ie. untested. I haven't messed with built-in
prototypes at all before.
for(p in this.element.prototype){
this[p] = this.element.prototype[p];
}
}
MyElement.prototype.update = function(html){
this.innerHTML=html;
};


Two. I could just be happy with functional programming

function updateElement(oElement, html) {
o.element.innerHTML = html;
};


What do you guys think or how do you approach this inability to use
built-in objects as prototypes?

Thanks,
Peter
 
R

RobG

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].


[...]
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.


[...]
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top