Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    102
    Thanks
    6
    Thanked 9 Times in 9 Posts

    test Object equality using data inside, not references. Possible?

    Is it possible to test whether two objects are equal using the data they contain inside and not comparing their pointers with ==?

    Well actually of course there is but...

    Is there a way to do it without actually looping through the object, instead maybe something that came with JS?
    (something like a .equals() method from other programming languages.)


    Thanks for any feedback!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Not sure that I understand the question, but this may help you:-

    Code:
    <input type = "text" id = "txt1" size = "20" onblur = "compare(this)">
    
    <script type = "text/javascript">
    
    function compare(which) {
    alert (which);   // [object]
    var x = which.value;
    alert (x);  // the value of txt1
    var y = "Philip";
    if (x == y) {
    alert ("That\'s right")
    }
    }
    </script>
    Or are you talking about an array object?
    If so, there is indexOf() which searches the array for specific elements, but this is implemented only in Firefox,
    not in IE. Prototype functions are available to make this method available to Internet Explorer and older browsers. For more info see:- http://www.hunlock.com/blogs/Masteri...ascript_Arrays



    "I think there is a world market for maybe five computers." — Thomas Watson, chairman of IBM, 1943.
    Last edited by Philip M; 01-29-2009 at 10:08 AM.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    two objects - even two empty objects- are never equal.

    You can give any constructor prototype a compare method,

    but you'll need to loop through both objects at some point.

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    but you'll need to loop through both objects at some point.
    - Aye, even the Object.equals function you refer to (in Java) runs through all the objects' properties behind the scenes.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #5
    Regular Coder
    Join Date
    Jun 2008
    Posts
    102
    Thanks
    6
    Thanked 9 Times in 9 Posts
    Okay, I'll just write one.
    I wanted to know if javascript had one built in because it would be much faster than what I could write (as js engine is in C++).

    Thanks for clearing that up

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    firefox has Object.toSource(), which produces identical string for identical objects without looping.
    it's VERY fast to.

    you can use something like .toJSONString(), or JSON.serialize() in when outside of firefox.

    it will do all the looping for you, and produce something that you can compare (2 strings).
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •