prototype property and onclick event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alhalayqa
    New Member
    • Mar 2008
    • 11

    prototype property and onclick event

    Hi,

    as you know, if I attach a property to the object prototype, it will be attached to all objects from the same instance.

    example :
    if I have a couple of images inside a document such as :
    <img src=""/>
    <img src=""/>
    <img src=""/>


    I can say : document.images[0].prototype.prin tMessage = myFunction ;
    where is :

    function myFunction(){ alert('Hello'); }

    now if I call : document.images[0].printMessage() , also
    document.images[1].printMessage() , ... all of them will alert "Hello".

    the question :

    if I have a couple of anchors inside a document, can I say :

    document.links[0].prototype.oncl ick = myFunction;


    such as when I click on that anchor by the mouse, the function would be called ? .



    thanks
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    Try it out, but first see what is returned when you
    alert(document. links[0].prototype)

    Comment

    • alhalayqa
      New Member
      • Mar 2008
      • 11

      #3
      Originally posted by mrhoo
      Try it out, but first see what is returned when you
      alert(document. links[0].prototype)
      I think you are right, try using __proto__ instead of prototype .
      I made a test case, its working, of course using the __proto__ .
      document.images[0].__proto__.prin tMessage = myFunction ;

      Comment

      Working...