DEV Community

yico
yico

Posted on

If( a==1 && a==2&&a==3 ) is equal to true under what circumstances?

Object.defineProperty()

//Using the principle of data hijacking, ES6 proxy can also be implemented
let bValue = 1;
Object.defineProperty(window, "a", {
  get() { return bValue++; }
});

if(a == 1 && a == 2 && a == 3) {
  console.log("Hello World!");
}
Enter fullscreen mode Exit fullscreen mode

Proxy

let test = {
  number: 1
};
test = new Proxy(test, {
  get(target, key) {
    return target[key]++;
  }
});
if(test.number === 1 && test.number ===2 && test.number ===3){
    console.log('Hello World!');
}
Enter fullscreen mode Exit fullscreen mode

Object.toString()

let a = [1, 2, 3];
//Use the array method shift to override toString, take the first element of the array and return
a.toString = a.shift;

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

//Override toString method
let a = {
    bValue: 1,
    toString: ()=> {
        return a.bValue++;
    }
}

if (a == 1 && a == 2 && a == 3) {
  console.log("Hello World!");
}
Enter fullscreen mode Exit fullscreen mode

Object.valueOf()

let a = [1, 2, 3];
//Use the array method shift to override valueOf, take the first element of the array and return
a.valueOf = a.shift;

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

/ **
* Override valueOf method
*/

let a = {
    bValue: 1,
    valueOf: ()=> {
        return a.bValue++
    }
};

if (a == 1 && a == 2 && a == 3) {
  console.log("Hello World!");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)