第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

js 判斷相等

js 判斷相等

陪伴而非守候 2019-04-08 11:18:22
>>>[1,2]==[1,2]false>>>[1,2]>[1,2]false>>>[1,2]>=[1,2]true>>>[1]==[1]false>>>1==[1]true求講解下這些原理
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

非常有意思的問(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),then
a.IfType(x)isUndefined,returntrue.
b.IfType(x)isNull,returntrue.
c.IfType(x)isNumber,then
i.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"#true
newString("a")==newString("a")#false
因此[1]==[1]實(shí)際上是兩個(gè)array對(duì)象的比較,如果兩個(gè)對(duì)象不指向同一個(gè)指針,那么則返回false
我們通過(guò)以下的小例子既可驗(yàn)證:
a=[1]
b=[1]
typeofa#Object
a==b#false
a==a#true
那么接下來(lái)解釋以下,為什么[1,2,3]>=[1,2,3]為什么就是true了呢?
以下是比較運(yùn)算符的算法定義:
Thecomparisonx
1.IftheLeftFirstflagistrue,then
a.LetpxbetheresultofcallingToPrimitive(x,hintNumber).
b.LetpybetheresultofcallingToPrimitive(y,hintNumber).
2.Elsetheorderofevaluationneedstobereversedtopreservelefttorightevaluation
a.LetpybetheresultofcallingToPrimitive(y,hintNumber).
b.LetpxbetheresultofcallingToPrimitive(x,hintNumber).
3.IfitisnotthecasethatbothType(px)isStringandType(py)isString,then
a.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,bothpxandpyareStrings
a.Ifpyisaprefixofpx,returnfalse.(AStringvaluepisaprefixofStringvalueqifqcanbetheresultofconcatenatingpandsomeotherStringr.NotethatanyStringisaprefixofitself,becauser
maybetheemptyString.)
b.Ifpxisaprefixofpy,returntrue.
c.Letkbethesmallestnonnegativeintegersuchthatthecharacteratpositionkwithinpxisdifferentfromthecharacteratpositionkwithinpy.(Theremustbesuchak,forneitherStringisaprefixoftheother.)
d.Letmbetheintegerthatisthecodeunitvalueforthecharacteratpositionkwithinpx.
e.Letnbetheintegerthatisthecodeunitvalueforthecharacteratpositionkwithinpy.
f.Ifm
NOTE1Step3differsfromstep7inthealgorithmfortheadditionoperator+(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());
};
                            
查看完整回答
反對(duì) 回復(fù) 2019-04-08
?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

我倒是不覺(jué)得這個(gè)問(wèn)題有什么好值得多說(shuō)的,我記得書上說(shuō)過(guò),只要記住兩個(gè)數(shù)組是不能直接進(jìn)行比較的,等于大于小于都不行。你看著數(shù)組里頭的值都是一樣的,但是這是偽數(shù)組,是對(duì)象,就比如說(shuō):
vararr=[1,2];
arr.test=function(){};
你覺(jué)得arr還會(huì)和[1,2]相等么。當(dāng)然兩個(gè)數(shù)組不能直接比較的原因會(huì)更復(fù)雜,我只是挑了一個(gè)比較好理解的地方說(shuō)了一下。
至于最后一個(gè)我想也比較好理解,因?yàn)槟闶褂玫氖?=不嚴(yán)格相等,所以如果兩邊比較類型不等的話會(huì)轉(zhuǎn)換至相等類型,因?yàn)榍耙粋€(gè)是1,類型是Number,所以js會(huì)對(duì)后一個(gè)[1]進(jìn)行類型轉(zhuǎn)換,Number([1])的結(jié)果是1,兩個(gè)自然就是相等的了。
                            
查看完整回答
反對(duì) 回復(fù) 2019-04-08
  • 2 回答
  • 0 關(guān)注
  • 453 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)