Then, for the type of typeof var!== "number", an attempt is made to convert to a 32-bit shape data, which is converted to Nan if it cannot be converted into a shaping data;
JS in-situ operation with a more convenient way to implement this operation, then its implementation principle can be generally understood as follows:
Copy Code code as follows:
var testdata=-2.9;
var testresult= (typeof testdata=== "number" &&!isnan (testData) &&testdata!==infinity)? (testdata>0)-math.floor (testData) -1:-math.ceil (testData) -1:-1;
First of all, if a data is <0 when it tries to convert to 32, then it needs to be rounded up, such as -2.9->-2, if >0, round it, such as:2.6->2;
If a data cannot be converted to a 32-bit binary representation, it is converted to NaN, and then to-1, such as ~{}/~nan ==-1;
Another example is ~function () {return 100;} ->-1;
In jquery, it works like if (!~this.classname.indexof (str)) {//do some thing ...}; Here, the return value for This.className.indexOf (str) is either greater than-1 or equal to 1; At its equal to-1, ~-1===0; then,!~-1===true; Then you can conclude that this does not contain the class name of STR ... ;
For the ~ ~ operator, similarly, it can also be expressed as:
Copy Code code as follows:
var testdata=2.1;
var testresult= (typeof testdata=== "number" &&!isnan (testData) &&testdata!==infinity)? (testdata>0)? Math.floor (TestData): Math.ceil (TestData): 0;
The same method of rounding up and down to understand;