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