So let’s do this one first
If (a==1&&a==2&&a=3){console.log(' printout succeeded '); }Copy the code
Method 1: Use the toString method.
=== === === === === === === ===
1. When an object == string, the object.toString() method converts the object to a string and compares 2. Null ==undefined, but not with other values. The rest is going to be converted to numbersCopy the code
So we can rewrite the toString method:
var a={ i=0; toString:(){ return ++this.i; }} if(a==1&&a==2&&a=3){console.log(' printable successfully '); }Copy the code
The toString method is called on each comparison, incrementing the value by 1, and then all are exactly equal.
Another way is to use the shift of the array
Var a = [1, 2, 3]; a.toString=a.shift(); If (a==1&&a==2&&a=3){console.log(' printable successfully '); }Copy the code
Method two, using data hijacking
For Object.defineProperty(), there are get() and set() methods. After listening for a value, get() is called each time the value is fetched and set() is called each time the value is set. Obj.defineproperty () can be used because each time a==1 we need to get the value of a first and then determine whether it is equal.
var i=0; var a; Object.defineProperty(window,'a',{ get(){ return ++i; }}) if(a==1&&a==2&&a=3){console.log(' printable successfully '); }Copy the code