The title as follows
var a = ? ; if(a == 1 && a == 2 && a == 3){ conso.log(1); }Copy the code
solution
1. Use the toString
let a = {
i: 1,
toString () {
return a.i++
}
}
if(a == 1 && a == 2 && a == 3) {
console.log('1');
}
Copy the code
2. Use the valueOf
let a = {
i: 1,
valueOf () {
return a.i++
}
}
if(a == 1 && a == 2 && a == 3) {
console.log('1');
}
Copy the code
An array of 3.
Var a = [1, 2, 3]; a.join = a.shift; if(a == 1 && a == 2 && a == 3) { console.log('1'); }Copy the code
4. ES6 symbol
let a = {[Symbol.toPrimitive]: ((i) => () => ++i) (0)};
if(a == 1 && a == 2 && a == 3) {
console.log('1');
}
Copy the code