题目
https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/57
var a = ?;
if(a == 1 && a == 2 && a == 3){
conso.log(1);
}
考察隐式类型转换
答案1
因为 == 会进行隐式类型转换,所以我们重写 toString 方法就可以了
var a = {
i: 1,
toString() {
return a.i++;
}
}
if( a == 1 && a == 2 && a == 3 ) {
console.log(1);
}
答案2
let a = {
i: 1,
valueOf() {
return a.i++;
}
}
if (a == 1 && a == 2 && a == 3) {
console.log(1);
}
答案3
let a = [1, 2, 3]
a.toString = a.shift;
if (a == 1 && a == 2 && a == 3) {
console.log(1);
}
答案。。
加粗样式