Original link: https: //www.jianshu.com/p/b7831b06f61a
Goof off at work when the brush to a very interesting article, you can take a look at the link above;
For me this white, this judgment conditions, it seems as if, possible, probably does not hold it. Then I carefully looked at (the first method),
The first pass, ah? What is the operation? ? ?
The second time, oh! So is the case (ps: I went to the open book --javaScript advanced tutorials equality operator that section)
ok, I figured out, when the object is to a rewriting equality operator before comparison type conversion --- toString
Etc., toString ???? clearly written book on object type conversion, the call is valueof () ah;
Practice makes perfect, of course, the code in order to figure out the knock
1, first knocked code again ↵ online
window.onload=function(){ var a = { i:1, toString:function(){ console.log('toString') return a.i++ } } if(a==1&&a==2&&a==3){ console.log('true') }else{ console.log(false) } }
Console output
Ah, it really is big brother, the code no problem then, success has been true; so, is it wrong book, in fact, is to convert the method call of toString ()? So, I knocked once the code
2, the toString () replaced valueOf () (1 • ㅂ •) و✧
window.onload=function(){ var a = { i:1, valueOf:function(){ console.log('valueOf') return a.i++ } } if(a==1&&a==2&&a==3){ console.log('true') }else{ console.log(false) } }
Console output
Oh roar! ! ! ! Also a success! ! !
Wait, I'm glad what kind of strength ah, why are successful ah
So, I had a third step operation
3, to override both methods to see which call
window.onload=function(){ var a = { i:1, valueOf:function(){ console.log('valueOf') return a.i++ }, toString:function(){ console.log('toString') return a.i++ } } if(a==1&&a==2&&a==3){ console.log('true') }else{ console.log(false) } }
Console output
EMM, it is obvious, it is the case where the rewritable call valueOf (), the present specification yes, the call object is valueOf () method, the type of conversion bit value
But why? (° ー ° 〃)
Why, just rewrite toString () when it is called
Well thought out. . . . .
So, I wrote a code rewrite removed all methods
window.onload=function(){ var a = { i:1 } if(a==1&&a==2&&a==3){ console.log('true') }else{ console.log(false) } }
Console output
Seems a little lost?
ok, plus a piece of code
window.onload=function(){ var a = { i:1 } console.log(a.valueOf()) console.log(a.toString()) if(a==1&&a==2&&a==3){ console.log('true') }else{ console.log(false) } }
Console output
Then I turn to such a passage in a book
ok, everything made sense, is defined as a target itself, like the so called valueOf numerical comparison, and returns the found value can not be converted to a value, so the call toString method, to give a [Object Object], very familiar with it, the result object transfer value, so the comparison was false
But because we rewrite valueOf and toString methods, the method has a return value of a numeric type, so == get the correct result of the comparison, the return true;
And because valueOf conversion before toString, so after the completion of the comparison is no longer calls the toString;
And because the default value valueOf get and can not be compared, so called after the rewrite toString method;
Completely ojbk (*
¯ ▽ ¯ *) O ----------------
Disclaimer: This article is CSDN blogger "and eaves private whisper 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/github_39371177/article/details/95450685