Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2995463.SPkdTlGXAF@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 16 Oct 2011 11:41:17 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Objects comparaison ? Newsgroups: comp.lang.javascript References: <1k93ek2.1xghqcy1upjvv5N%unbewusst.sein@fai.invalid> <1783522.fYvYJRLvp5@PointedEars.de> <1411612.7OFYzboFZ6@PointedEars.de> <2326223.jLKWLtnpvF@PointedEars.de> <2065619.34vx2sxqLb@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 74 NNTP-Posting-Date: 16 Oct 2011 11:41:20 CEST NNTP-Posting-Host: 3f12f337.newsspool4.arcor-online.net X-Trace: DXC=adf>LA=dI33T2Rfi6 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(). > 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 -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs,