Extending JavaScript Element "class"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluehtrombone
    New Member
    • Mar 2008
    • 1

    Extending JavaScript Element "class"

    Hi everybody,

    I am trying to extend the JavaScript's native class Element as below. If I then create two or more distinct objects, assigning values to any of them affects all the objets as if they were referencing one and the same object. However, div1 == div2 throws false. Could anybody explain why is that and how to fix this issue?

    Code:
    function Div()
    {
      //Empty
    }
    
    Div.prototype = document.createElement("DIV");
    
    var div1 = new Div();
    var div2 = new Div();
    
    div1.style.border = "solid 1px black";
    div2.style.border = "dashed 4px red";
    
    document.body.appendChild(div1);
    Thanks!
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    Originally posted by bluehtrombone
    div1 == div2 throws false.
    Could anybody explain why is that and how to fix this issue?

    Thanks!


    objects are passed by ref, not by value, and because div1 and div2 are different objects, they are not equivalent to javascript.

    consider the simplest test case:
    Code:
     var a={a:1}
     var b={a:1}
     alert(a==b)
    it shows false, because objects don't compare like primitives.

    Comment

    Working...