JavaScript 中 (a == 1 && a == 2 && a == 3) 可能为 true 吗?

这是 Stack Overflow 上面的一个问题,乍一看 a == 1 && a == 2 && a == 3 根本不可能嘛。然后看下面答案,觉得真是妙。 目前最高赞的答案是这个:

const a = {
i: 1,
toString: function () {
return a.i++;
}
}

if(a == 1 && a == 2 && a == 3) {
console.log(‘Hello World!’);
}

排在后面的分别是这几个答案:

var i = 0;

with({
get a() {
return ++i;
}
}) {
if (a == 1 && a == 2 && a == 3)
console.log(“wohoo”);
}

with({
get a() {
return Math.floor(Math.random()*4);
}
}){
for(var i=0;i<1000;i++){
if (a == 1 && a == 2 && a == 3){
console.log(“after “+(i+1)+” trials, it becomes true finally!!!”);
break;
}
}
}

var val = 0;
Object.defineProperty(window, ‘a’, {
get: function() {
return ++val;
}
});
if (a == 1 && a == 2 && a == 3) {
console.log(‘yay’);
}

  最后附上 Stack Overflow的链接 不过我觉得如果谁真的在开发项目中使用这个的话,会被打死的吧。