Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

js ==比较类型转换的问题?

先跑一遍三个小题目, 是否符合自己的预期

0==[]
![]==[]
'0'==[]

如果有大佬 全答对,请解释一波 '0'==[] 这个是怎么做比较的;
另外附上能解释 第1,2题目 的文章
https://ostack.cn/a/11...
第三题实在不懂...求解释~~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

image.png

数组(也是对象)和数字、字符串比较,要转为字符串后比较。
Array检查valueOf,没有返回原始值(返回的是[]),所以调用toString,得到空串'', '' == '0'false


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...