Forum Moderators: open

Message Too Old, No Replies

How to prototype all document objects

         

vol7ron

3:47 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



I can do something like this:

// function definition
document.foo = function(bar){alert(bar);}

// function call
document.foo(3); // messagebox that outputs '3'


However, I want to prototype something similar for all document objects/elements (tables, input boxes, etc).

The following doesn't work:

// don't work
Object.foo = function(bar){alert(bar);}
document.Object.foo = function(bar){alert(bar);}

// works for new Object()
Object.prototype.foo = function(bar){alert(bar);}

var obj = getElementById('ElementsID'); // typeof obj == "object"
obj.foo(2); // doesn't work

var obj = new Object();
obj.foo(3); // does work


Prototyping only works on new objects created by the Objects() class, but not document objects, or those objects that already exist. I'm not exactly sure how to event reference the document.[object collection]

I've tried to search the net, but the 'Prototype' package returns more hits than the prototype method/attribute. If anyone knows how to do this, I would be very grateful.


Thank you,
vol7ron



.

vol7ron

4:24 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



basically what is needed is some way to reference the element class.

document.foo = function(){...}
element.foo = function(){...} // obviously this isn't right
document.element.foo = ... // this also wouldn't work

vol7ron

7:20 pm on Mar 11, 2010 (gmt 0)

10+ Year Member



The only way I know how to do this is to loop (most likely a for-loop) through all the elements and set the value there. However, this is unwanted because (1) it would create a new function for each element, rather than one function and (2) it would not be applied to newly created elements.

The ideal thing would be something like:
<element class reference>.prototype.foo = function(){...}
<object class reference>.prototype.foo = function(){...}

This way the function would be applied to all elements/objects, but again, I'm still unsure how to reference the base class constructor for elements.

daveVk

5:29 am on Mar 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[ruby-forum.com...]

Dom elements are not true js objects in IE.