Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!news.internetdienste.de!news.tu-darmstadt.de!news.belwue.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1783522.fYvYJRLvp5@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 13 Oct 2011 21:21:33 +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> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 75 NNTP-Posting-Date: 13 Oct 2011 21:21:33 CEST NNTP-Posting-Host: d6fab8d7.newsspool2.arcor-online.net X-Trace: DXC=PO8fL9`H^^CE4ZB2flKORAA9EHlD;3YcB4Fo<]lROoRA8kF 13.10.2011 21:20, Une Bévue wrote: >> 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. >> 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. 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 -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16