1. Precedence of Operators
The reason for the function () {} () Report syntax error:
Unary operatoris the right-order priority combination. So your expression is equivalent to: function () ({} ())//Practice error is the same, so should: (function () {}); But why (function () {} ()) Do not error and can execute it? Grouping operators:
Main uses:
1, with the comma operator for assignment. for example: var num = (5,1,4,8,0); Here the last value of num is 0.
2. Convert to an expression. such as eval (' (' +jsstr+ ') '), or:
function fn () {
}//function declaration, cannot be called directly
(function fn () {
});//use () to enclose the function, you can call directly
3, for calling functions. such as FN ();.
The operator is left-to-right: 2+3+4
Category |
Operator |
Describe |
Description |
Unary operator |
++ |
Self-increment 1 |
1, since the increase (minus) there are two types of pre-and post-set, the first self-increment (minus) and then participate in other operations, the first to participate in other operations and then self-increment (minus). 2, in Es, the self-increment (minus) applies not only to integers, they can be used for any value, for a value that is not of type number, it is implicitly converted to numbers by the rules in the previous article, and then by Increment (minus), the variable type also becomes number type. |
-- |
Self minus 1 |
+ |
One dollar Plus |
The primary application of unary Plus is to convert the operand to the number type, which is equivalent to calling the # () conversion. |
- |
One Yuan minus |
The one-dollar minus is the opposite number on the basis of one dollar plus. |
Arithmetic operators |
+ |
Add |
1. In addition to plus (+), if the operand is not of type number, it will be automatically converted to the # type and then evaluated. 2, for addition and subtraction (+-), except as an arithmetic operator. It can also be used as a unary operator (see above). Of course, because of the overloading of the plus sign (+) in the string operation, it can also be used to connect any numeric value (the string), which is also the 1th why in addition (+), when it contains a non-number type value, it will convert all operands to a string connection. 3, unlike the General C language, in Es, except (/) and modulo (%) does not distinguish between integers and floating-point numbers, such as 5/2 = 2.5 instead of 2,5.3% 3 = 2.3 instead of 2. 4, arbitrary operation, as long as the operand contains Nan, the result is Nan. But it's not as if the result is Nan there must be an operand of Nan, for example 0/0 also returns Nan. 5, for the operation with infinite infinity, the provisions are more, here is not listed, can refer to the original book, or self-test. |
- |
Reducing |
* |
By |
/ |
Except |
% |
Take the mold |
logical operators (Boolean operator) |
! |
Logical Non- |
First, the operand is converted to a Boolean value and then reversed. can use double non!! Converts a value to the corresponding Boolean value. |
&& |
logic with |
1, returns True when the Boolean value corresponding to two operands is true 2, Short circuit: When the corresponding Boolean value of the first operand is false, false is returned directly, and the second operand is no longer evaluated. This is often used to determine if a variable (property) is defined, such as: if (object && object.name && object.name = ' Linjisong ') {} This will be the first to judge the existence of an object, does not exist will not parse object.name thereby preventing the occurrence of errors, as well as only object.name exist, will be to compare this value. |
| | |
Logical OR |
1, returns true if at least one of the two operands has a Boolean value of True 2, Short circuit: True is returned directly when the corresponding Boolean value of the first operand is true, and the second operand is no longer evaluated. 3, logical, or, in addition to general judgment, is often applied in cases where the default value is provided, such as function Fn (obj) { obj = obj | | {}; } If the call to FN is not passed in to obj, obj is automatically assigned a value of undefined, and because the corresponding Boolean value of undefined is false, an empty object {} is assigned to obj, if the call is passed to obj, Because the Boolean value of an arbitrary object is true, the subsequent {} is not taken to achieve the effect of a default value of obj {}. The is also applied to a number of relatively separate files in a large JS library: //jslib var jslib; File1 (function (jslib) { Jslib = Jslib | | {}; }) (Jslib); //file2 (function (jslib) { Jslib = Jslib | | {}; }) (Jslib); This way, regardless of which file is loaded first, the jslib is defined, and if not defined, provides a default value, which allows a relatively independent module to take the load order. |
Relational operators (comparison operator) |
< |
Less than |
1. If one operand is a number type or a Boolean value, the two operands are converted to the number type value (if conversion is required) to perform a numeric comparison. 2, string comparison, character encoding values are compared one by one. 3, when the operator is an object, call valueof () (if not, call ToString ()), and then compare the result to the rule above. 4, any number and Nan compare return false. |
<= |
Less than or equal to |
> |
Greater than |
>= |
Greater than or equal to |
== |
Equal |
1, equal and unequal (= =,! =) in comparison, implicit type conversions are required whenever necessary. 2, congruent and not congruent (= = =,!==) in comparison, will not convert the type, if the type is inconsistent, directly for the!==. 3, combined 1, 2, you can know, a===b must have a==b, and a!=b must have a!==b. |
!= |
Range |
=== |
Congruent |
!== |
Not congruent |
Assignment operator |
= |
Assign value |
|
Compound Arithmetic assignment operator |
Arithmetic operator plus = |
Corresponding arithmetic operators, with + =,-=, *=,/=,%= |
Compound Bitwise assignment Operator |
Bitwise operator PLUS = |
Corresponds to bitwise operators, with ~=, &=, |=, ^=, <<=, >>=, >>>= |
Bitwise operators |
~ |
Bitwise non- |
bitwise inverse, which returns the inverse code |
& |
Bitwise-AND |
Bitwise-ALIGNED, bitwise-only, only two operation bits are 1 to return 1, otherwise the bit returns 0, and finally the combination of all bit operation results is returned |
| |
Bitwise OR |
Bitwise-ALIGNED, bitwise-only, only two operation bits are 0 to return 0, otherwise the bit returns 1, and finally the combination of all bit operation results is returned |
^ |
Bitwise XOR OR |
Bitwise-ALIGNED, bit-wise, two operation bits are different when 1 is returned, otherwise the bit returns 0, and all the bit operation results are combined back |
<< |
Move left |
Binary number shifted left, left shift does not change symbol bit |
>> |
Signed Right Shift |
Binary number shifted right, high to match bit fill |
>>> |
Unsigned Right Shift |
The binary number shifts right, shifts right, and for positive numbers, the result is the same as >>, and for negative numbers, the binary complement of negative numbers is treated as a positive binary code. |
String operator |
+ |
String connection |
The equivalent of the concat () function, which converts all operands to a string before connecting. Note that once a string is created, it does not change, and when a string connection is performed, there is an intermediate connection and destruction process in the background, which is why the old browser is running slow when a large number of string connections are in operation. |
+= |
String CONCATENATE composite |
A+=b, equivalent to A=a+b. |
Object operators |
. |
Property accessors |
A simple object property accessor. |
[] |
property or (Class) array access |
by [], you can access the name is a variable or an attribute that contains special characters. |
New |
Call the constructor to create the object |
Returns a newly created object that is inside the constructor that is pointed to by this newly created object. |
Delete |
Variable, property Delete |
Delete an attribute (a variable can be considered a global object or a property of the execution environment). |
void |
|
Returns undefined. |
Inch |
Judging attributes |
An object property or a property on the prototype chain. |
instanceof |
Prototype judgment |
Compares whether an object in the same context is a prototype of another object. |
Other operators |
?: |
Conditional operator |
syntax; var value = exp? Trueexp:falseexp. equivalent to Var value; if (exp) {value = Trueexp;} Else{value = Falseexp;} |
, |
Comma operator |
It is mainly used to declare multiple variables, which is a popular practice in many JS libraries. For example: Var num1=1,num2=2,num3=3; |
() |
Grouping operators |
Main uses: 1, with the comma operator for assignment. For example: var num = (5,1,4,8,0); Here the last value of num is 0. 2. Convert to an expression. such as Eval (' (' +jsstr+ ') '), or: function fn () { }//function declaration, cannot be called directly (function fn () { });//use () to enclose the function, you can call directly 3, for calling functions. such as FN ();. |
typeof |
Type operator |
Returns a String value: undefined type, ' undefined ', null type ' object ', Boolean type ' Boolean ', Number type ' number ', string- > ' string ', instance of built-in function object, ' function ', other object type ' object '. (Some browser implementations are slightly different) |
Explain the points:
1, the classification is not very strict, such as bitwise NON (~), logical non (!), delete, void, typeof, can be regarded as a unary operator, and self-increment (+ +) in a lot of data is also classified as arithmetic operators. I am in the collation of the main reference to the original book classification, but also take into account the natural nature.
2, plus (+) usage is more flexible, it should be noted, especially for the calculation, ignoring the string, it is easy to make mistakes.
3, typeof is generally used to judge the simple data type, if it is the object type, because most of the return is object, not much practical use, and instanceof judgment also need to meet the conditions of the same context, otherwise it will be wrong, The judgment of the object class will explain the other more secure method later when the object is described.
4, first look at the following code:
Copy CodeThe code is as follows:
var numval = 10.00,
Strval = ' 10 ',
Strobj = new String (' 10 ');
Console.info (Numval = = strval);//true
Console.info (typeof (Strval+strobj));//string
The first output is actually true, is not unexpected to you? Here, because the = = Comparer has an implicit type conversion, the number type is converted to a string type, and then a value of type 10.00 is parsed into an integer 10 because there are no values that are not 0 after the decimal point, so the comparison is equal. The second output is a string, which is actually relatively easy to understand, Strval is a string, Strobj is a string object, the two add, will convert the object into a string, so the final result is a string type.
5, about the symbol, repeat a few popular usage (here does not involve the use of regular expressions):
(1) Use unary Plus (+) to convert to number type.
(2) Use dual logic non (!! ) is converted to a Boolean type.
(3) Use logic and (&&) to detect the existence and subsequent operation of the object.
(4) Use logic or (| | ) to provide a default value for the function parameter.
(5) Use Grouping (()) to explicitly specify as an expression.
(6) Use curly braces ({}) to define object literals, JSON data formats, and code blocks.
(7) Use the brackets ([]) to define the array literal, JSON data format, access to the array, and the access name is the attribute of the variable or special character.
6, about the bitwise operation, although the results are not very intuitive, but the operation of high efficiency, there are many interesting applications, such as not using intermediate variables to exchange two values directly, judging odd and even, MD5 encryption, and so on, interested friends can find relevant data to study themselves.