-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Open
Labels
Description
题目如下
var a = ?;
if(a == 1 && a == 2 && a == 3){
conso.log(1);
}
答案解析 因为==会进行隐式类型转换 所以我们重写toString方法就可以了
var a = {
i: 1,
toString() {
return a.i++;
}
}
if( a == 1 && a == 2 && a == 3 ) {
console.log(1);
}
pengpwang, chenfaxiang, Vikingama, ablikim915, kukuxi and 331 more16302010080, lily7129 and SchengSteinsyearnwin, NathanHan1, TrrantChen, Buzz888, Myth-0909 and 12 moreleoni121, xu909486480, weineel, wenqizou, Myth-0909 and 5 moreleoni121, Myth-0909, 2118180500, GinaChenxiaoyu, diyanshan and 2 morecoveyz, 1517060322, JillyGacai, leoni121, np2472059195 and 8 moreWindyNanzi, Xu-Angel, dense1, eoit3, Myth-0909 and 7 more
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Yanhua67 commentedon Mar 21, 2019
jefferyE commentedon Mar 21, 2019
这题考察的应该是类型的隐式转换,考引用类型在比较运算符时候,隐式转换会调用本类型toString或valueOf方法.
解答:
Moriarty02 commentedon Mar 21, 2019
这个题目考察==的隐式转换吧
从 (a==1&&a==2&&a==3) 成立中看javascript的隐式类型转换
win7killer commentedon Mar 21, 2019
win7killer commentedon Mar 21, 2019
要改下,不然报错。你这个挺好,可以做 ===
jjeejj commentedon Mar 21, 2019
精简一下代码:
HankXu commentedon Mar 21, 2019
一开始没转过来,群里老哥提了一声才想到这个方向
XinJack commentedon Mar 21, 2019
第一反应想到的是a = console.log(1)😂😂
jiyuzhuang commentedon Mar 22, 2019
@XinJack 感觉你这个才是最优解啊,半天说不出一句话。
RicoLiu commentedon Mar 27, 2019
@XinJack console.log() 的返回值是 undefined
leehomeok commentedon Mar 28, 2019
数组这个 a.join = a.shift; 没看懂啊
jjeejj commentedon Mar 28, 2019
把 shift 方法的引用 ,放到 a.join 上的。覆盖原来的 join 方法
seujzhang commentedon Mar 29, 2019
为啥a==1之后会执行join(shift)函数?
jjeejj commentedon Mar 30, 2019
@seujzhang 执行
a ==1
会进行隐式转换seujzhang commentedon Mar 30, 2019
小白求教下,是不是这样的:在执行a==1的时候,会尝试对a进行隐式转换,此时隐式转换会调用Array的join方法,而此时join方法被shift覆盖,所以调用的实际上是shift方法,弹出1,然后相等,再弹出2相等,弹出3相等,最后console执行。
40 remaining items
weblijingang commentedon Jul 22, 2020
jackchang2015 commentedon Jul 30, 2020
MrLeihe commentedon Apr 22, 2021
覆盖原型还是不太好
DemonKHH commentedon Apr 25, 2021
这样不香嘛
zhelingwang commentedon May 8, 2021
Y-J-H commentedon May 30, 2021
最先想到的是get, set, 这样好像就没有var a了, 感觉有点偏题, 不过估计知识点应该是valueOf, toString, 哈哈哈
nxt-hj commentedon Jul 16, 2021
Ha0ran2001 commentedon Aug 19, 2021
Boolean和其他类型比较,先被转换为Number,true被转换成 1
Ha0ran2001 commentedon Aug 19, 2021
@ihoneys 啊?隐式类型转换还会调用 join 方法,我还以为就 toString 和 valueOf 呢,还有就是那个 拆箱时ToPrimitive,还会调用哪些方法?
qinyakang commentedon Aug 30, 2021
这个在那个环境运行的,我咋没打印出来1 啊
LeeRayno commentedon Sep 5, 2021
再精简一下:
tchen-l commentedon Sep 10, 2021
这都没有进 if 语句啊😂
Yuweiai commentedon Jul 13, 2022
ECMAScript
中的相等和不等操作符会在执行比较之前,先将对象转换成相似的类型。后来,有人提出了这种转换到底是否合理的质疑ECMAScript
的解决方案就是提供两组操作符:==
)和不相等(!=
)在转换不同的数据类型时,遵循下列基本规则:a = true
不行的原因)valueOf()
方法,用得到的基本类型值按照前面的规则进行比较;**如果valueOf()
也得不到基本类型值,则会调用toString()
;**如果toString()
也得不到基本类型值,则会报错:TypeError: Cannot convert object to primitive value
lete114 commentedon Jul 21, 2022
JDongKhan commentedon Aug 7, 2023
估计设计语言的时候,也没想到被这群屌人玩的这么花。