prototype and dom elements

C

codefragment

Noddy question for someone but...
I can use prototype to add a method to a class, if an object is
created locally it works but I can't add methods to objects already
existing in the dom?
Sure someone somewhere has written about this but the nature of the
keywords prototype and
class make it difficult to search for

thanks for any help

<body>
<form>

<input id=theButton type=button onclick="func(this)" />

<script>
Object.prototype.bob = function()
{
alert('bob')
}

function func(elem)
{
var theButton = document.getElementById('theButton');

var obj = new Object();
obj.bob(); // works

theButton.bob(); // doesn't work
}


</script>

</form>
</body>
 
C

codefragment

ah, just noticed, this is IE only (7 in my case). In firefox it works.
Anyone know a workaround (behaviours or some IE quirk perhaps? Won't
proceed with this until later tonight so any suggestions in the
meantime welcome
 
S

SAM

Le 12/29/08 3:12 PM, (e-mail address removed) a écrit :
ah, just noticed, this is IE only (7 in my case). In firefox it works.
Anyone know a workaround (behaviours or some IE quirk perhaps? Won't
proceed with this until later tonight so any suggestions in the
meantime welcome


function func(elem)
{
elem.toString().bob(); // works (IE6, FF3)
}

function func(elem)
{
elem.id.bob(); // works (IE6, FF3)
}

function func(elem)
{
elem.o = ''
elem.o.bob(); // works (IE6, FF3)
}

Why ?
I don't know.
 
T

Thomas 'PointedEars' Lahn

<input id=theButton type=button onclick="func(this)" />
<script>
Object.prototype.bob = function()
{
alert('bob')
}

function func(elem)
{
var theButton = document.getElementById('theButton');

var obj = new Object();
obj.bob(); // works

theButton.bob(); // doesn't work
}

<http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>

In few DOMs (I know of only one to support it, the Gecko DOM, but I have yet
to test others thoroughly), host objects, like those implementing the
HTMLInputElement interface, inherit from the native object being referred to
by Object.prototype. For example, the MSHTML DOM does not support this
feature. We have discussed this already, yesterday at the latest; STFW.

BTW, your test case is not Valid. <http://validator.w3.org/>


PointedEars
 
C

codefragment

the nature of the keywords prototype and class make it difficult to
search for
<http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>
 We have discussed this already, yesterday at the latest; STFW.

Unless I read this newsgroup (I don't) I would have no way of knowing
that. As I said in the
original post as theres a prototype library out there I couldn't find
any decent keywords to search
for that didn't result in matches for the library and not for this.

As for the other moan I provided a standalone example and detailed the
problem (can't add methods
to the DOM objects) so as far as I'm concerned thats all I'd expect
from a poster.

Conclusion: Thats life. I can work around the problem so I'll guess
I'll have to
 
C

codefragment

Le 12/29/08 3:12 PM, (e-mail address removed) a écrit :


function func(elem)
{
        elem.toString().bob(); // works (IE6, FF3)

}

function func(elem)
{
        elem.id.bob(); // works (IE6, FF3)

}

function func(elem)
{
   elem.o = ''
   elem.o.bob();  // works (IE6, FF3)

}

Why ?
I don't know.

I think those examples work because your now dealing with a different
object, namely a string, which I
guess doesn't have the same problem. However as its a different object
it wouldn't be useful in my scenario.

Thanks anyway
 
J

JR

Hi,
Dean Edwards alerts in his "Rules For JavaScript Library Authors":

Rule #2 - Object.prototype is verboten!
This is so important that it needs a rule all to itself. Objects
are the basic building blocks of JavaScript functionality. Don’t mess
with them.

http://dean.edwards.name/weblog/2007/03/rules/

Hope this helps.

Joao Rodrigues
 
C

codefragment

Rule #2 - Object.prototype is verboten!
    This is so important that it needs a rule all to itself. Objects
are the basic building blocks of JavaScript functionality. Don’t mess
with them.

http://dean.edwards.name/weblog/2007/03/rules/

I totally agree believe it or not. I'm just doing this to test a
facebook application I've
written. It won't be part of the actual program
I'll check out the link, thanks
 
R

RobG

the nature of the keywords prototype and class make it difficult to
search for


Unless I read this newsgroup (I don't) I would have no way of knowing
that.

If you had browsed postings for the last few days before posting, as
is etiquette, you may.

As I said in the
original post as theres a prototype library out there I couldn't find
any decent keywords to search
for that didn't result in matches for the library and not for this.

Yes, it is silly to name a library after a keyword of the language it
is built on, but there you go.

You would have more luck searching this group using terms like "extend
DOM object" or "DOM object prototype"
 
T

Thomas 'PointedEars' Lahn

the nature of the keywords prototype and class make it difficult to
search for


Unless I read this newsgroup (I don't) I would have no way of knowing
that.

That's Usenet. Read/research first, write later. In fact, nobody really
wants to read and answer the same question more than one time; there are
more important, more interesting things to do.
As I said in the
original post as theres a prototype library out there I couldn't find
any decent keywords to search
for that didn't result in matches for the library and not for this.

Fair enough. "prototype dom elements" would have given viable results,
though. Knowledge requires research, and research requires knowledge on
how to do research. Get used to it.
As for the other moan

I can't believe what I'm reading. You have been given good advice on how
to do better next time. You have been given a second chance, and you
blew it.
I provided a standalone example [...]

That's it. I really don't have time for your whining. Why don't you find
a nice parking clock to talk to?


PointedEars
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top