非常有意思的問(wèn)題~首先,比較符號(hào)有如下幾種:==|===|!=|!==|>|>=|<|<=先說(shuō)一下===,其表示嚴(yán)格相等,首先比較類型,再比較值,如果都相同,則返回True,這個(gè)都木有什么問(wèn)題的~比較簡(jiǎn)單,一帶而過(guò)了。主要討論下面一種比較類型:接著說(shuō)一下==,其表示probablyequal.可以理解模糊判等。在ECMA-262中對(duì)其做了如下定義:TheproductionEqualityExpression:EqualityExpression==RelationalExpressionisevaluatedasfollows:1.LetlrefbetheresultofevaluatingEqualityExpression.2.LetlvalbeGetValue(lref).3.LetrrefbetheresultofevaluatingRelationalExpression.4.LetrvalbeGetValue(rref).5.Returntheresultofperformingabstractequalitycomparisonrval==lval.(see11.9.3).在11.9.3中具體定義了相等算法Thecomparisonx==y,wherexandyarevalues,producestrueorfalse.Suchacomparisonisperformedasfollows:1.IfType(x)isthesameasType(y),thena.IfType(x)isUndefined,returntrue.b.IfType(x)isNull,returntrue.c.IfType(x)isNumber,theni.IfxisNaN,returnfalse.ii.IfyisNaN,returnfalse.iii.IfxisthesameNumbervalueasy,returntrue.iv.Ifxis+0andyis-0,returntrue.v.Ifxis-0andyis+0,returntrue.vi.Returnfalse.d.IfType(x)isString,thenreturntrueifxandyareexactlythesamesequenceofcharacters(samelengthandsamecharactersincorrespondingpositions).Otherwise,returnfalse.e.IfType(x)isBoolean,returntrueifxandyarebothtrueorbothfalse.Otherwise,returnfalse.f.Returntrueifxandyrefertothesameobject.Otherwise,returnfalse.2.Ifxisnullandyisundefined,returntrue.3.Ifxisundefinedandyisnull,returntrue.4.IfType(x)isNumberandType(y)isString,returntheresultofthecomparisonx==ToNumber(y).5.IfType(x)isStringandType(y)isNumber,returntheresultofthecomparisonToNumber(x)==y.6.IfType(x)isBoolean,returntheresultofthecomparisonToNumber(x)==y.7.IfType(y)isBoolean,returntheresultofthecomparisonx==ToNumber(y).8.IfType(x)iseitherStringorNumberandType(y)isObject,returntheresultofthecomparisonx==ToPrimitive(y).9.IfType(x)isObjectandType(y)iseitherStringorNumber,returntheresultofthecomparisonToPrimitive(x)==y.10.Returnfalse.NOTE1Giventheabovedefinitionofequality:*Stringcomparisoncanbeforcedby:""+a==""+b.*Numericcomparisoncanbeforcedby:+a==+b.*Booleancomparisoncanbeforcedby:!a==!b.NOTE2Theequalityoperatorsmaintainthefollowinginvariants:*A!=Bisequivalentto!(A==B).*A==BisequivalenttoB==A,exceptintheorderofevaluationofAandB.NOTE3Theequalityoperatorisnotalwaystransitive.Forexample,theremightbetwodistinctStringobjects,eachrepresentingthesameStringvalue;eachStringobjectwouldbeconsideredequaltotheStringvaluebythe==operator,butthetwoStringobjectswouldnotbeequaltoeachother.ForExample:*newString("a")=="a"and"a"==newString("a")arebothtrue.*newString("a")==newString("a")isfalse.NOTE4ComparisonofStringsusesasimpleequalitytestonsequencesofcodeunitvalues.Thereisnoattempttousethemorecomplex,semanticallyorienteddefinitionsofcharacterorstringequalityandcollatingorderdefinedintheUnicodespecification.ThereforeStringsvaluesthatarecanonicallyequalaccordingtotheUnicodestandardcouldtestasunequal.IneffectthisalgorithmassumesthatbothStringsarealreadyinnormalisedform.請(qǐng)注意:1.f中提到Returntrueifxandyrefertothesameobject.Otherwise,returnfalse.其表示如果x和y都指向了同一個(gè)對(duì)象返回true,否則返回false嗒嗒~~再來(lái)舉個(gè)例子newString("a")=="a"#truenewString("a")==newString("a")#false因此[1]==[1]實(shí)際上是兩個(gè)array對(duì)象的比較,如果兩個(gè)對(duì)象不指向同一個(gè)指針,那么則返回false我們通過(guò)以下的小例子既可驗(yàn)證:a=[1]b=[1]typeofa#Objecta==b#falsea==a#true那么接下來(lái)解釋以下,為什么[1,2,3]>=[1,2,3]為什么就是true了呢?以下是比較運(yùn)算符的算法定義:Thecomparisonx1.IftheLeftFirstflagistrue,thena.LetpxbetheresultofcallingToPrimitive(x,hintNumber).b.LetpybetheresultofcallingToPrimitive(y,hintNumber).2.Elsetheorderofevaluationneedstobereversedtopreservelefttorightevaluationa.LetpybetheresultofcallingToPrimitive(y,hintNumber).b.LetpxbetheresultofcallingToPrimitive(x,hintNumber).3.IfitisnotthecasethatbothType(px)isStringandType(py)isString,thena.LetnxbetheresultofcallingToNumber(px).Becausepxandpyareprimitivevaluesevaluationorderisnotimportant.b.LetnybetheresultofcallingToNumber(py).c.IfnxisNaN,returnundefined.d.IfnyisNaN,returnundefined.e.IfnxandnyarethesameNumbervalue,returnfalse.f.Ifnxis+0andnyis-0,returnfalse.g.Ifnxis-0andnyis+0,returnfalse.h.Ifnxis+∞,returnfalse.i.Ifnyis+∞,returntrue.g.Ifnyis-∞,returnfalse.k.Ifnxis-∞,returntrue.l.Ifthemathematicalvalueofnxislessthanthemathematicalvalueofny—notethatthesemathematicalvaluesarebothfiniteandnotbothzero—returntrue.Otherwise,returnfalse.4.Else,bothpxandpyareStringsa.Ifpyisaprefixofpx,returnfalse.(AStringvaluepisaprefixofStringvalueqifqcanbetheresultofconcatenatingpandsomeotherStringr.NotethatanyStringisaprefixofitself,becausermaybetheemptyString.)b.Ifpxisaprefixofpy,returntrue.c.Letkbethesmallestnonnegativeintegersuchthatthecharacteratpositionkwithinpxisdifferentfromthecharacteratpositionkwithinpy.(Theremustbesuchak,forneitherStringisaprefixoftheother.)d.Letmbetheintegerthatisthecodeunitvalueforthecharacteratpositionkwithinpx.e.Letnbetheintegerthatisthecodeunitvalueforthecharacteratpositionkwithinpy.f.IfmNOTE1Step3differsfromstep7inthealgorithmfortheadditionoperator+(11.6.1)inusingandinsteadofor.NOTE2ThecomparisonofStringsusesasimplelexicographicorderingonsequencesofcodeunitvalues.Thereisnoattemptousethemorecomplex,semanticallyorienteddefinitionsofcharacterorstringequalityandcollatingorderdefinedintheUnicodespecification.ThereforeStringvaluesthatarecanonicallyequalaccordingtotheUnicodestandardcouldtestasunequal.IneffectthisalgorithmassumesthatbothStringsarealreadyinnormalisedform.Also,notethatforstringscontainingsupplementarycharacters,lexicographicorderingonsequencesofUTF-16codeunitvaluesdiffersfromthatonsequencesofcodepointvalues.[1,2]>[1,2]的比較實(shí)際上是首先轉(zhuǎn)換成字符串,然后進(jìn)行字符串比較。[1,2]>[1,2]#實(shí)際上轉(zhuǎn)換成了'1,2'>'1,2'返回false[1,2]>=[1,2]#返回true同樣的道理,1==[1],因?yàn)?不是object,所以[1]要轉(zhuǎn)換成number來(lái)進(jìn)行比較,因此返回true那么如何來(lái)判斷兩個(gè)array是否相等呢?a=[1,2,3]b=[1,2,3]#只要a不大于b并且a不小于b不就說(shuō)明a等于b嘛,因此可以構(gòu)造函數(shù)如下:functionarrays_equal(a,b){return!(aarrays_equal([1,2,3],[1,2,3])#true還有一個(gè)比較trick的方法arraysSimilar=function(arr1,arr2){returnJSON.stringify(arr1.sort())===JSON.stringify(arr2.sort());};