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>