Then, for the type of typeof var!==”number”, when performing operations, it will try to convert into 32-bit integer data. If it cannot be converted into integer data, it will be converted into NaN;
JS uses bitwise operations A simpler way to implement this operation, then its implementation principle can be roughly understood 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 trying to be converted to 32 integer data, the result is < ;0, then it needs to be rounded up, such as -2.9->-2, if >0, it is rounded down, such as: 2.6->2;
If a data cannot be converted to 32 bit binary representation, it is converted to NaN; then converted to -1; for example ~{}/~NaN ==-1;
and for example ~function(){return 100;}->-1;
In Jquery, it is useful to use if(!~this.className.indexOf(str)){ //do some thing…..}; Here, the return value of this.className.indexOf(str) is either greater than -1 , or it is equal to -1; when it is equal to -1, ~-1===0; then, !~-1===true; then it can be concluded that this does not contain the class name str...;
For the ~~ operator, in the same way, it can also be expressed as:
var testData=2.1;
var testResult=(typeof testData==="number"&&!isNaN(testData)&&testData!==Infinity)?(testData>0)?Math.floor(testData): Math.ceil(testData):0;
is also understood by rounding up and down;