-Write down some tips on operators or error prone areas
% operator
The sign of the result of the operation of the remainder operator is determined by the sign of the first operator, for example:
1 // -1 2 // 1
So sometimes errors can occur with negative values, and using the absolute value function avoids errors:
// will go wrong function isodd (n) {return n% 2 = = = 1;} IsOdd (// false// false// correct function isodd (n) {return math.abs (n% 2) = = = 1;} IsOdd (// true// false
+ operator
The + operator is not the same as other operators, and we know that it can be used to concatenate string operations because it usually converts other types of values to strings when using the + operator, but other types of values are converted to numeric values, except for example, operators, such as:
var New Date (); typeof // "string" typeof // "Number"
When the object appears in the operator:
1 + [+]// "11,2"1 + {a:1}// "1[object object"
The valueof method of the object is called first. If the return result is a value of the original type, it is converted to a string, otherwise the ToString method of the object is called and then converted to a string.
But:
{a:1} + 1// 1({a:1}) +1 "[Object Object]1]
What is this for? At this point {a:1} is treated as a block of code, and the code block does not return a value, so the entire expression returns 1. But the {a:1} is placed in parentheses because JS expects () to be a value, so it is treated as an object again.
* * Special expression: * *
1. Empty array + empty array
Call valueof () first to return the empty array itself, and then call ToString () to return an empty string.
[] + []// ""
2. Empty array + Empty object
[] get ', {} get ' [Object Object] '
[] + {}// "[Object Object]"
3. Empty object + empty array
{} is seen as a block of code, +[] is the meaning of converting [] to a number 0.
{} + []// 0
4. Empty object + Empty object
The same {} is omitted as a block of code, +{} turns to a value of Nan
{} + {}// NaN
If the first empty object is not treated as an empty block of code:
({}) + {}// "[Object Object][object Object]"+ {})// "[ Object Object][object Object] "+ {})// " [Object Object][object Object] " var a = {} + {};a// "[Object Object][object Object]"
In addition, when the + operator is placed in front of other values as a numeric operator, it can be used to convert any value to a numeric value, as in the number function:
+True// 1// 0// NaN
! Take the inverse operator
! The inverse operator continuously deserializes the same value by converting it to its corresponding Boolean value, as in the case of a Boolean function:
!! x // equivalent to Boolean (x)
Also, if we want to exclude null for this object, you can write:
if (!! x) {//dosomething!}
This is because:!! The null value is False, the other object!! The obj value is true.
~ No operator
The ~ operator is calculated from the binary binary form of the value.
// -4
Its operation principle is based on the value of the 32-bit binary integer form operation, the principle of the complement of the store if it is negative, you need to reverse the value of the inverse and then minus.
It's a bit cumbersome, but we can remember that a value added to its inverse value equals-1.
~~2.9// 2
The two-time no operation is capable of rounding decimals, and this is the fastest method of rounding.
^ Xor operator
Two times XOR the value of two numbers:
var a = ten; var b = ^=b, B^=a, a^=/ ///
<< left shift operator
Move left 0-bit to rounding:
13.5 << 0// -13.5 << 0// -13
The left shift operation converts the RGB value of the color to the hex value:
var color = {r:186, g:218, b:85}; // RGB to HEX var function (R, G, b) {return ' # ' + ((1 <<) + (R << +) + (g << 8) + B). ToString (+). substr (1);} Rgb2hex (color.r,color.g,color.b)// "#bada55"
>> Right Shift operation
The right shift operation simulates the 2 divide operation:
// equivalent to 5/2 = 2 // equivalent to 21/4 = 5 // equivalent to 21/8 = 2 // equivalent to 21/16 = 1
In addition, the void operator is used to execute an expression and then return undefined, and its operator precedence is also higher than void 4+7 actually equivalent to (void 4) +7. The general operators are left-associative, but = and three-mesh operators? : It's right-associative:
w = x = y == a?b:c?d:e? f:g;
= (x = (y == a?b: (c?d: (e?f:g));
Some tips in the operator