|
Prev: xmlHttpRequest gives no response
Next: “OpenOffice.org” free Officesuite for Microsoft Windows, Linux, Solaris, BSD, OpenVMS, OS/2 or IRIX
From: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= on 19 Apr 2008 14:33 lets say i have two objects with the same properties and rge same values : var o1={element:one, type:'keyup',code:82,action:setToRed}; var o2={element:one, type:'keyup',code:82,action:setToRed}; the variable "one" being a DIV Element and "setToRed" a function. doing alert(o1===o2) gave me false. does it means the comparaison operates over references not "values" ??? I need to have a proper comparator, saying "eql" for Objects containing any king of values : Number, string, date, object... how could i know an Object is of type <tag name> Element ? only by testing obj.nodeName ? -- Une B�vue
From: Joost Diepenmaat on 19 Apr 2008 14:52 unbewusst.sein(a)weltanschauung.com.invalid (Une Bévue) writes: > lets say i have two objects with the same properties and rge same values > : > > var o1={element:one, type:'keyup',code:82,action:setToRed}; > var o2={element:one, type:'keyup',code:82,action:setToRed}; > > the variable "one" being a DIV Element and "setToRed" a function. > > doing alert(o1===o2) gave me false. > > does it means the comparaison operates over references not "values" > ??? Depends on the type of object. For the *exact* meaning, see ecma-262 section 11.9.6. Simplified (IOW incorrect; rule of thumb): if o1 and o2 are both strings or (not NaN) numbers, o1 === o2 is true if they are of the same value. If o1 and o2 are of type Object, it's only true if they are the *same* object. > I need to have a proper comparator, saying "eql" for Objects containing > any king of values : > > Number, string, date, object... There isn't any such comparator. IME the you only actually need such a very general comparator in very specific library routines such as general object serializers. You don't really need it in day-to-day programming. > how could i know an Object is of type <tag name> Element ? > > only by testing obj.nodeName ? Seems like a fairly good choice, assuming I understand you correctly. How does that relate to the rest of your post? -- Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
From: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= on 19 Apr 2008 15:39 Joost Diepenmaat <joost(a)zeekat.nl> wrote: > > There isn't any such comparator. IME the you only actually need such a > very general comparator in very specific library routines such as > general object serializers. You don't really need it in day-to-day > programming. Ok, that's clear to me now, i have to compare only object like that : var o1={element:one, type:'keyup',code:82,action:setToRed,modifiers:{shift:false,alt:false}}; then with Strings, Numbers Simple Objects having boolean values, string or number, Function (in this case "setToRed") and DOM Element (in this case "one" is a DIV). > > how could i know an Object is of type <tag name> Element ? > > > > only by testing obj.nodeName ? > > Seems like a fairly good choice, assuming I understand you > correctly. How does that relate to the rest of your post? because i need to compare objects having DOM Element as value of a property and experimetally i've found the comparaison between to DOM Elements is correct, surprisingly (ie toto=document.getElementById('toto') is "===" to anotherToto=document.getElementById('toto'). in the above ewample the way i do the "comparator" is (simplified) : - compare with null both sides; - if both objects have the property 'nodeName' I'll compare the object identity like that : anObject===anotherObject - if not and it is an object, I'll compare all property/value pairs recursively, like that : if(!(this[p].eql(o[p]))){return false;} (my "comparator" is a prototype of Object : Object.prototype.eql=function(o){...};) the reason for this[p].eql(o[p]) replacing "this[p]===o[p]" then i needed to know the best way to say 'this object is of type DOM Element'... -- Une B�vue
From: Thomas 'PointedEars' Lahn on 19 Apr 2008 15:45 Joost Diepenmaat wrote: > unbewusst.sein(a)weltanschauung.com.invalid (Une Bévue) writes: >> lets say i have two objects with the same properties and rge same values >> : >> >> var o1={element:one, type:'keyup',code:82,action:setToRed}; >> var o2={element:one, type:'keyup',code:82,action:setToRed}; >> >> the variable "one" being a DIV Element and "setToRed" a function. >> >> doing alert(o1===o2) gave me false. >> >> does it means the comparaison operates over references not "values" >> ??? > > Depends on the type of object. Actually, it does not. Since objects have identity, if there are two different objects, both the `==' and `===' operations will result in `false', no matter their constructor or prototype object. > For the *exact* meaning, see ecma-262 section 11.9.6. Two references are equal if they refer to the same object. As simple as that. > Simplified (IOW incorrect; rule of thumb): if o1 and o2 are both strings > or (not NaN) numbers, o1 === o2 is true if they are of the same > value. If o1 and o2 are of type Object, it's only true if they are the > *same* object. Strings and numbers are _not_ objects, they are primitive values. But String *objects* and Number *objects* are objects, and the result of the `==' and `===' operations is for them as with any object: // false new String("x") == new String("x") Never confuse primitive values and objects in ECMAScript implementations, especially avoid using an object where a primitive value suffices; if you need the object's properties, you can use them in an expression anyway, for example: var x = 42.3234; // 2 x.toFixed(2).toString().length // 97 "a".charCodeAt(0) PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
From: Joost Diepenmaat on 19 Apr 2008 16:09
unbewusst.sein(a)weltanschauung.com.invalid (Une Bévue) writes: > Joost Diepenmaat <joost(a)zeekat.nl> wrote: > >> >> There isn't any such comparator. IME the you only actually need such a >> very general comparator in very specific library routines such as >> general object serializers. You don't really need it in day-to-day >> programming. > > Ok, that's clear to me now, i have to compare only object like that : > > var o1={element:one, > type:'keyup',code:82,action:setToRed,modifiers:{shift:false,alt:false}}; > > > then with Strings, Numbers Simple Objects having boolean values, string > or number, Function (in this case "setToRed") and DOM Element (in this > case "one" is a DIV). in that case you could compare each property in that list using === except, possibly, for the DOM element - see below. > because i need to compare objects having DOM Element as value of a > property and experimetally i've found the comparaison between to DOM > Elements is correct, surprisingly (ie > toto=document.getElementById('toto') is "===" to > anotherToto=document.getElementById('toto'). It's not all that surprising, since most browsers probably really do use the same host object to refer to the same DIV. But as far as I know, they're not *required* to do so, and even if they did, I'm not sure that host objects are required to be === to themselves (though still, as far as implementing them goes, it's probable that they are). If you're using valid markup, and the DIVs you're comparing all have ID properties and are in the same document, you could just compare the ID, since IDs should be unique. IOW: document.getElementById("bla").id == document.getElementById("bla").id > in the above ewample the way i do the "comparator" is (simplified) : > > - compare with null both sides; > - if both objects have the property 'nodeName' I'll compare the object > identity like that : anObject===anotherObject This might not be safe. See above. > - if not and it is an object, I'll compare all property/value pairs > recursively, like that : > if(!(this[p].eql(o[p]))){return false;} As far as I can see from your types of properties, you can just do if (this[p] !=== o[p]) return false; > (my "comparator" is a prototype of Object : > Object.prototype.eql=function(o){...};) the reason for this[p].eql(o[p]) > replacing "this[p]===o[p]" I don't think you need that. > then i needed to know the best way to say 'this object is of type DOM > Element'... In this case, I'd just check if the object as an id property; if so, compare that, otherwise do object1 === object2. If you can control your environment enough, that should work. If you can't control your environment, that may not, but in that case, there's probably no general solution. -- Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/ |