javascript - Can (a= 1 > a =2 > a=3) jamais évalué à true

Mots clés : javascriptecmascript-6javascript

meilleur 5 Réponses javascript - Can (a= 1 > a =2 > a=3) jamais évalué à true

vote vote

91

const a = {    i: 1,    toString: function () {      return a.i++;    }  }    if(a == 1 && a == 2 && a == 3) {    console.log('Hello World!');  }
vote vote

90

var aᅠ = 1;  var a = 2;  var ᅠa = 3;  if(aᅠ==1 && a== 2 &&ᅠa==3) {      console.log("Why hello there!")  }
var a_ = 1;  var a = 2;  var _a = 3;  if(a_==1 && a== 2 &&_a==3) {      console.log("Why hello there!")  }
var a= 1;  var a‍= 2; //one zero-width character  var a‍‍= 3; //two zero-width characters (or you can use the other one)  if(a==1&&a‍==2&&a‍‍==3) {      console.log("Why hello there!")  }
vote vote

73

var i = 0;    with({    get a() {      return ++i;    }  }) {    if (a == 1 && a == 2 && a == 3)      console.log("wohoo");  }
  var i = 0;      with({      get a() {        return ++i;      }    }) {      if (a !== a)        console.log("yep, this is printed.");    }
vote vote

69

a = [1,2,3];  a.join = a.shift;  console.log(a == 1 && a == 2 && a == 3);
let i = 0;  let a = { [Symbol.toPrimitive]: () => ++i };    console.log(a == 1 && a == 2 && a == 3);
vote vote

55

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;      }    }  }

Questions similaires