Objects comparaison ?

U

Une Bévue

if i do :
var b = ({ 'huit': 8} == { 'huit': 8})? true : false;

i get b = false

however doing :
var o = { 'huit': 8};
b = (o == o)? true : false;
gives obviously true meaning objects are compared by reference ?

not true for strings :

var a = 'toto';
var c = 'toto';
var b = (a===c)? true : false;

gives true...

is there a function like equal for objects ???
or does I need to implement it ?
 
J

Jukka K. Korpela

13.10.2011 21:20 said:
if i do :
var b = ({ 'huit': 8} == { 'huit': 8})? true : false;

i get b = false

Yes, because each object literal creates a new object and a reference to it.
however doing :
var o = { 'huit': 8};
b = (o == o)? true : false;
gives obviously true meaning objects are compared by reference ?

You can say so if you like (stay tuned to the resident troll giving a
pointless lecture on this), but basically what you are comparing here is
values of expressions, and the value of "o" is the same as the value of
"o", namely a reference to an object.
not true for strings :

var a = 'toto';
var c = 'toto';
var b = (a===c)? true : false;

gives true...

Right, because strings are not objects. For strings, even the strict
equality operator (===) is defined in terms of equality of characters
(or, technically, the 16-bit values) in the strings. The types must
match too, but two identical string literals compare as equal, and so do
variables initialized by identical string literals.
is there a function like equal for objects ???
or does I need to implement it ?

If you wish to compare objects by equality of property values, you'll
have to do it yourself (directly or via libraries). This is not quite as
trivial as it may sound, as you need to decide whether it suffices that
all properties of the first object exist in the second object too, with
identical values (with some value for "identical"), or whether you also
wish to require that the objects are intuitively copies of the same
stuff, so that the second object has no properties that the first one
hasn't.
 
T

Thomas 'PointedEars' Lahn

Jukka said:
13.10.2011 21:20 said:
however doing :
var o = { 'huit': 8};
b = (o == o)? true : false;
gives obviously true meaning objects are compared by reference ?

[…] basically what you are comparing here is values of expressions, and
the value of "o" is the same as the value of "o", namely a reference to an
object.

The *evaluation* value of the expression `o' is a reference to one object.
Equal references compare equal, like any other value. But, as references
are not type-converted in that context, by contrast it does not matter
whether you use loose or strict comparison.
not true for strings :

var a = 'toto';
var c = 'toto';
var b = (a===c)? true : false;

gives true...

Right, because strings are not objects. […]

*Primitive* *string* *values* are not objects (they are converted to them if
context requires it). Change this to

var a = new String('toto');
var c = new String('toto');
var b = (a === c)? true : false;

and it will yield `false' (with or without the strict comparison), because
`a' and `c' would store different references (references to different
objects).

BTW,

var b = (a === c);

is semantically equivalent, but more efficient. Don't make booleans if you
already have them.
If you wish to compare objects by equality of property values, you'll
have to do it yourself (directly or via libraries). This is not quite as
trivial as it may sound, as you need to decide whether it suffices that
all properties of the first object exist in the second object too, with
identical values (with some value for "identical"), or whether you also
wish to require that the objects are intuitively copies of the same
stuff, so that the second object has no properties that the first one
hasn't.

Java's equals() method – which exists precisely for that purpose – should
provide a good start for an implementation in ECMAScript. But as the
existence of properties cannot be reliably tested in a
(backwards-)compatible way in ECMAScript implementations, this only leaves
either comparing the results of property accesses for the same property name
or losing that compatibility.

However, practice shows that it is seldom necessary to compare objects for
equality; it usually suffices, and is more efficient, to compare key
property values or object references instead.


PointedEars
 
J

Jukka K. Korpela

13.10.2011 22:21 said:
The *evaluation* value of the expression

The resident troll behaved in the expected way of posting something
totally irrelevant, though slightly unexpectedly using his own "terms".
Not accepting the simple concept of "value", he just had to invent
"evaluation value". That's like sexy sex, or computed calculation.
Java's equals() method

Someone might event think that the troll, like many novices, cannot
distinguish Java from JavaScript. But of course he just intentionally
obfuscates things (stay tuned to his usual babbling about why
"JavaScript", the word used by the rest of the world, is all wrong).
 
T

Thomas 'PointedEars' Lahn

Jukka said:
The resident troll behaved in the expected way of posting something
totally irrelevant,

Not everything you don't get is irrelevant.
Someone might event think that the troll, like many novices, cannot
distinguish Java from JavaScript. But of course he just intentionally
obfuscates things (stay tuned to his usual babbling about why
"JavaScript", the word used by the rest of the world, is all wrong).

Unlike people who can read, you still continue to amaze with your childish
ignorance and failed attempts at scoring where there really is nothing to
score (except in my scorefile).


PointedEars
 
D

dhtml

Jukka said:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
[whines]
[whines]

Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.

The /guidelines/ for equality appeared in Effective Java, by Josh
Bloch many years ago. These guidelines made it into JavaDoc. Here:

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)

(Nobody but nobody cares about that stupid score-keeping business.)
 
T

Thomas 'PointedEars' Lahn

dhtml said:
Thomas said:
Jukka said:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that purpose –
should provide a good start for an implementation in ECMAScript.]

Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.

I did not write `Object.equals()', if you cared to notice.
[irrelevance]
 
U

Une Bévue

Thomas 'PointedEars' Lahn said:
However, practice shows that it is seldom necessary to compare objects for
equality; it usually suffices, and is more efficient, to compare key
property values or object references instead.

that's right, I'll do that way, same properties with equal values and
also I'll limit recursivety to my needs ))

Surprisingly i got another trick :

var huit = { 'huit' : 8};
var ary = [ 0, 1, 3, 'trois', huit];

I wanted to test my #include method on array :

ary.include( 3 ); // -> true
ary.include( 2 ); // -> false
....

AND :

ary.include( huit ); // -> false

-UN-understandable to me !

I need that to work because I have an Array of observers being objects
created by constructor and I need to detach observer from the model :

#detach( observer );

in the Model() part of MVC :

function detach( observer ) {

for( var i=0, l=this.observers.length; i < l; i++){
if(this.observers == observer) {
this.observers.splice( i , 1 );
}
}

....
}

and the observer being a View() :

function View( Observable ){
Observable.attach( this );
...
}

then i wonder if it would be more efficient to add a property (or
method) to each observer (View() object) some kind of unique hash|ID

and makes comparaison on that :

anObserver#equal(anotherObserver);

function equal(another) {
return ( this.hash|ID == another.hash|ID );
}

And then the above mentionned :
if(this.observers == observer) {

would become :
if(this.observers.equal(observer)) {

???
 
U

Une Bévue

Jukka K. Korpela said:
Which "#include method"?

The one I've added to Array :
Array.prototype.include=function(s){
for(var i=0,l=this.length;i<l;i++){if(this===s){return true;}}
return false;
}
 
J

Jukka K. Korpela

14.10.2011 08:31 said:
Jukka K. Korpela said:
Which "#include method"?

The one I've added to Array :
Array.prototype.include=function(s){
for(var i=0,l=this.length;i<l;i++){if(this===s){return true;}}
return false;
}


Tested with the code you posted earlier,

var huit = { 'huit' : 8};
var ary = [ 0, 1, 3, 'trois', huit];

ary.include( huit ) returns true, as expected. So how did you test it
when you got false?
 
U

Une Bévue

Une Bévue said:
Surprisingly i got another trick :

var huit = { 'huit' : 8};
var ary = [ 0, 1, 3, 'trois', huit];

I wanted to test my #include method on array :

ary.include( 3 ); // -> true
ary.include( 2 ); // -> false
...

AND :

ary.include( huit ); // -> false

Sorry for the noise, may be i had a typo in my code...

I get the RIGHT thinks :
var huit = { 'huit': 8};
ary = [ 0, 1, 2, 3, 'cinq', 6, 7, huit];
[ 0, 1, 2, 3, 'cinq', 6, 7, { 'huit': 8 } ].include({ 'huit': 8 }) =
true

or, in my reading...
 
U

Une Bévue

Jukka K. Korpela said:
Tested with the code you posted earlier,

var huit = { 'huit' : 8};
var ary = [ 0, 1, 3, 'trois', huit];

ary.include( huit ) returns true, as expected. So how did you test it
when you got false?

That's right !
Sorry for the noise, I've re-re-tested it and it works !!!
 
D

dhtml

dhtml said:
Thomas said:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that purpose–
should provide a good start for an implementation in ECMAScript.]
Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.

I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?
 
T

Thomas 'PointedEars' Lahn

dhtml said:
Thomas said:
dhtml said:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that purpose –
should provide a good start for an implementation in ECMAScript.]

Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.

I did not write `Object.equals()', if you cared to notice.

Which Java equals() method did you mean?

Context matters. By "should provide a good *start* for an implementation
*in* ECMAScript" I was referring to the *concept* of Java's Object.equals()
method, a method which is *overriden* by subclasses.

The OP was wondering about the difference between comparing two object
references and two primitive strings in an ECMAScript implementation.
Consider then, for example, String.equals(), which is necessary in Java
because *there* *are* *no* *primitive* *string* *values* *in* *Java*, and
`x == "foo"' would be useless (it would always result in `false'). So you
need to do x.equals("foo") or – better¹ – "foo".equals(x) in Java [1] for
what you need only `==' in ECMAScript.

So comparing strings, for example, is more complicated in Java. But by
comparison ECMAScript's approach also is less powerful and less flexible
because the way the comparison is performed there is more or less written in
stone, defined by the algorithms in the Specification.

The concept of an equals() method, on the other hand, carries with it the
necessity and the *possibility* for the software developer of defining what
makes objects (of different types) equal, i. e. *which* field/property
values of objects are compared against each other and what defines *their*
(in)equality, which is what Jukka Korpela pointed out.

There are also other programming languages which implement this concept and
similar ones; more recently Python, where the algorithm for the `==', `<',
and `>' operations between objects can be defined by a __cmp__() method of
the class of either object [2].

This concept can be implemented in an ECMAScript implementation, like
JavaScript, as well; but, without sacrificing efficiency in existing
implementations, [AFAIK] only in a Java-like way, so without the pythonic
sugar.


HTH

PointedEars
___________
¹ IMHO "better" because we know that "foo" will evaluate to a new String
instance in Java but we do not know about `x'.

[1]
<http://download.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)>
[2] <http://docs.python.org/release/2.5.2/ref/customization.html>
 
A

Antony Scriven

dhtml said:
Thomas said:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [which exists
precisely for that purpose should provide
a good start for an implementation in
ECMAScript.]
Java `Object.equals` does nothing more than use
Java's == which is analagous to JavaScript's ===.
I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?

Context matters. By "should provide a good *start* for
an implementation *in* ECMAScript" I was referring to the
*concept* of Java's Object.equals() method, a method
which is *overriden* by subclasses. [...snip nonsense...]

Thomas, that was a rhetorical question (or it should have
been). --Antony
 
D

dhtml

dhtml said:
Thomas said:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that purpose –
should provide a good start for an implementation in ECMAScript.]
Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.
I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?

Context matters.  By "should provide a good *start* for an implementation
*in* ECMAScript" I was referring to the *concept* of Java's Object.equals()
method, a method which is *overriden* by subclasses.
Not necessarily. Object `equals` method may be overridden but this is
often neglected by programmers. Java guidelines for how to override
`equals` are applicable here.
The OP was wondering about the difference between comparing two object
references and two primitive strings in an ECMAScript implementation.  
Two different String objects can never be == as they are different
objects. Two primitive strings may be == or ===. A primitive string
and a string object may be == but never ===.

var A1 = new String("a");
var A2 = new String("a");
var a = "a";

a == A1; // true
a == A2; // true
A1 == A2; // false

a === A1; // false

You know this, but not all readers will.
Consider then, for example, String.equals(), which is necessary in Java
because *there* *are* *no* *primitive* *string* *values* *in* *Java*, and
`x == "foo"' would be useless (it would always result in `false').  

Not always. Depends if it'd been interned. But that's not relevant to
our discussion. Anyway:

So you
need to do x.equals("foo") or – better¹ – "foo".equals(x) in Java [1] for
what you need only `==' in ECMAScript.
Right because in Java, a null String `x`, where x.equals("foo") might
be a NPE.
So comparing strings, for example, is more complicated in Java.  But by
comparison ECMAScript's approach also is less powerful and less flexible
because the way the comparison is performed there is more or less writtenin
stone, defined by the algorithms in the Specification.
RIght.
 
T

Thomas 'PointedEars' Lahn

dhtml said:
Thomas said:
dhtml said:
Thomas 'PointedEars' Lahn wrote:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that
purpose – should provide a good start for an implementation in
ECMAScript.]
Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.
I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?

Context matters. By "should provide a good *start* for an implementation
*in* ECMAScript" I was referring to the *concept* of Java's
Object.equals() method, a method which is *overriden* by subclasses.

Not necessarily.

Most definitely it is. There is String.equals() and, say, Boolean.equals().
Both are subclasses of Object and override Object.equals().
Object `equals` method may be overridden but this is often neglected by
programmers. Java guidelines for how to override `equals` are applicable
here.
ACK
Consider then, for example, String.equals(), which is necessary in Java
because there are no primitive string values in *Java*, and
`x == "foo"' would be useless (it would always result in `false').

Not always. Depends if it'd been interned. […]
ACK
The OP was wondering about the difference between comparing two object
references and two primitive strings in an ECMAScript implementation.
Two different String objects can never be == as they are different
objects. Two primitive strings may be == or ===. A primitive string
and a string object may be == but never ===.

var A1 = new String("a");
var A2 = new String("a");
var a = "a";

a == A1; // true
a == A2; // true
A1 == A2; // false

a === A1; // false

You know this, but not all readers will.

My point was the difference when comparing values of the same type, with
different types each. But yes, it is valuable to know that, in ECMAScript,
object references are internally converted to primitive values (through 9.1
ToPrimitive and 9.3 ToNumber, and eventually a native object's toString() or
valueOf() methods) when loosely compared against primitive values.


PointedEars
 
D

dhtml

dhtml said:
Thomas said:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that
purpose – should provide a good start for an implementationin
ECMAScript.]
Java `Object.equals` does nothing more than use Java's == which is
analagous to JavaScript's ===.
I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?
Context matters.  By "should provide a good *start* for an implementation
*in* ECMAScript" I was referring to the *concept* of Java's
Object.equals() method, a method which is *overriden* by subclasses.
Not necessarily.

Most definitely it is.  There is String.equals() and, say, Boolean.equals().  
Both are subclasses of Object and override Object.equals().
And there is also java.lang CharSequence and StringBuffer, which do
not. And also:
So there really isn't a Java equals method that applies to the OP.
There is Java's guidelines though, to be reflexive, symmetric,
transitive, and consistent. An `equals` method should also have no
observable side effects.
 
T

Thomas 'PointedEars' Lahn

dhtml said:
Thomas said:
dhtml said:
Thomas 'PointedEars' Lahn wrote:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
dhtml wrote:
Thomas 'PointedEars' Lahn wrote:
Jukka K. Korpela wrote:
13.10.2011 22:21, Thomas 'PointedEars' Lahn wrote:
Java's equals() method [– which exists precisely for that
purpose – should provide a good start for an implementation
in ECMAScript.]
Java `Object.equals` does nothing more than use Java's == which
is analagous to JavaScript's ===.
I did not write `Object.equals()', if you cared to notice.
Which Java equals() method did you mean?
Context matters. By "should provide a good *start* for an
implementation *in* ECMAScript" I was referring to the *concept* of
Java's Object.equals() method, a method which is *overriden* by
subclasses.
Not necessarily.
Most definitely it is. There is String.equals() and, say,
Boolean.equals(). Both are subclasses of Object and override
Object.equals().

And there is also java.lang CharSequence and StringBuffer, which do
not.

What are you getting at? Two subclasses are *two* *subclasses*.
And also:


So there really isn't a Java equals method that applies to the OP.

Apparently you are unable to understand that I was referring to the
*concept* of having such a method. Perhaps I should consider that my
fault: I should have explained it in simpler terms, or refrained from
replying to you in the first place :->

Consider this:

Object.equals = function(obj1, obj2) {
if (typeof obj1 != "object" || obj1 == null
|| typeof obj2 != "object" || obj2 == null)
{
return (obj1 === obj2);
}

for (var propertyName in obj1)
{
if (obj1[propertyName] !== obj2[propertyName])
{
return false;
}
}

return true;
};

/* true */
Object.equals({'huit': 8}, {'huit': 8});

Yes, that's error-prone and incomplete and all that, but it's a *start*!
There is Java's guidelines though, to be reflexive, symmetric,
transitive, and consistent. An `equals` method should also have no
observable side effects.

You don't say!

Mindless crosspost ignored.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top