473,508 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

alternatives to built-in objects as prototypes

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

Jul 21 '06 #1
1 1556
pe**********@gmail.com wrote:
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.
--
Rob

Jul 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1912
by: yawnmoth | last post by:
popen / proc_open seem to be broken (as per PHP Bug #30463). As such, does anyone know if there are any alternatives to them? I tried to use fsockopen to load a bunch of instances of a script to...
0
2066
by: Paul | last post by:
Does anyone know of any alternatives to dadabik? I have looked at dadabik and it looks good but I wanted something which had better facilities for different users. The security for dadabik is a...
2
1444
by: someuser91 | last post by:
Does anybody know about any alternatives to sourceforge? It was a great service once but now they rejecting all kinds of good projects. I think they are running out of servers support or...
1
2193
by: Miles Davenport | last post by:
I would like some advice on what Java server-side alternatives their are to an applet which is a shopping cart application which allows the user to drag-and-drop individual items into "an order"...
1
1896
by: adsheehan | last post by:
Hi folks, I notice that M2Crypto (a python wrap of OpenSSL) leaks (haemorrhages) memory significantly and affects my long running app very badly. Does anyone know of fixes to this problem? ...
4
369
by: ME | last post by:
What are the .NET Compact Framework Alternatives to Enum.Parse and Enum.Format as they are not supported by the Compact Framework? Thanks, Matt
3
1291
by: someuser91 | last post by:
Hi guys, Does anybody know any alternatives to sourceforge? It was a great service once but now they rejecting all kinds of good projects. I think they are running out of servers or something.
11
2631
by: binarybana | last post by:
After recently getting excited about the possibilities that stackless python has to offer (http://harkal.sylphis3d.com/2005/08/10/multithreaded-game-scripting-with-stackless-python/) and then...
5
2883
by: webguynow | last post by:
I like Smarty but I may have to design something similar myself, bc I'm having trouble getting it installed on my Shared WebHost. There are good examples out there. One on CodeWalkers...
1
3094
by: Spencer | last post by:
Our Mainframe DBA insists that the IDTHTOIN parameter be set to 600 so that all idle threads timeout after 10 minutes. This is causing a particular packaged application that expects to hold idle...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.