...

View Full Version : test Object equality using data inside, not references. Possible?



hotwheelharry
01-29-2009, 07:18 AM
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!

Philip M
01-29-2009, 08:54 AM
Not sure that I understand the question, but this may help you:-


<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/Mastering_Javascript_Arrays



"I think there is a world market for maybe five computers." — Thomas Watson, chairman of IBM, 1943.

mrhoo
01-29-2009, 02:50 PM
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.

itsallkizza
01-29-2009, 04:39 PM
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.

hotwheelharry
01-30-2009, 04:15 AM
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 :thumbsup:

rnd me
01-30-2009, 07:24 AM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum