470,590 Members | 2,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

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?

Expand|Select|Wrap|Line Numbers
  1. function Div()
  2. {
  3.   //Empty
  4. }
  6. Div.prototype = document.createElement("DIV");
  8. var div1 = new Div();
  9. var div2 = new Div();
  11. div1.style.border = "solid 1px black";
  12. div2.style.border = "dashed 4px red";
  14. document.body.appendChild(div1);
Mar 10 '08 #1
1 1350
rnd me
427 Expert 256MB
div1 == div2 throws false.
Could anybody explain why is that and how to fix this issue?


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:
Expand|Select|Wrap|Line Numbers
  1.  var a={a:1}
  2.  var b={a:1}
  3.  alert(a==b)
it shows false, because objects don't compare like primitives.
Mar 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

32 posts views Thread by Will Hartung | last post: by
5 posts views Thread by Rob | last post: by
3 posts views Thread by Giggle Girl | last post: by
2 posts views Thread by Angus | last post: by
4 posts views Thread by rebeccatre | last post: by
36 posts views Thread by Roedy Green | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.