JavaScript advanced programming Reading Notes (5) Operators in ECMAScript (1) _ javascript skills

Source: Internet
Author: User
ECMAScript operator. If you are studying JavaScript, you can check the 2.9 operator.
2.9.1 unary Operators
The unary operator has only one parameter, that is, the object or value to be operated.

1. delete: delete references to previously defined object attributes or methods. However, this operator cannot delete undefined attributes and methods. Example:

The Code is as follows:


Var o = new Object;
O. name = "Nicolas ";
O. show = function (){
Return "test ";
};
Console. log (o. name); // outpus Nicolas
Console. log (o. show (); // outpus test

Delete o. name;
Delete o. show;

Console. log (o. name); // outpus undefined
Console. log (o. show (); // outpus TypeError: o. show is not a function

Delete o. toString;
Console. log (o. toString (); // outpus [object Object]


2. void: Return undefined for any value. This operator is usually used to avoid output of values that should not be output.

For example, to open a new window in the link, the Code is as follows:

Click Me
Click the link and the [object] will appear in the new window. This is because the window. open () method returns a reference to the newly opened window. The object is then converted to the string to be displayed. To avoid this result, you can use the void operator to call the window. open () function:

Click Me
3. Pre-increment/pre-increment operators: two operators borrowed from C. Example:

The Code is as follows:


Var iNum = 10;
Console. log (++ iNum); // outpus 11 same as iNum = iNum + 1
Console. log (iNum); // outpus 11
Console. log (-- iNum); // outpus 10 same as iNum = iNum-1
Console. log (iNum); // outpus 10


4. Post-increment/post-increment operators: two operators borrowed from C. Like the increment or decrement operation, the increment or decrement operation also adds 1 or 1 to the value. The difference is that the suffix operator performs the increment or decrement operation only after the expressions that contain them are calculated. Example:

The Code is as follows:


Var iNum = 10;
INum --
Console. log (iNum); // outpus 9
Console. log (iNum --); // outpus 9
Console. log (iNum); // outpus 8
INum ++
Console. log (iNum); // outpus 9
Console. log (iNum ++); // outpus 9
Console. log (iNum); // outpus 10


5. One-dimensional addition and one-dimensional subtraction: The usage is the same as that in high school mathematics. One-dimensional addition has no effect on the number. One-dimensional Subtraction is to calculate the negative value. However, when performing operations on a string using the unary return method and unary subtraction method, it is similar to parseInt (). The main difference is that only the string starting with "ox" is used, the unary operator converts it to a decimal value. Example:

The Code is as follows:


Var iNum = 25;
INum = + iNum;
Console. log (iNum); // outpus 25

Var sNum = "50 ";
Console. log (typeof sNum); // outpus string
Console. log (+ sNum); // outpus 50
Console. log (typeof + sNum); // outpus number

Var sNum1 = "017 ";
Var sNum2 = "0xB ";
Console. log (-sNum1); // outpus-17
Console. log (-sNum2); // outpus-11


2.9.2-bit Operators
1. Bit operation NOT: (~) The process is as follows:

(1) convert the number of operations to a 32-bit number

(2) convert the binary form into its binary anticode;

(3) convert binary anticode to floating point

Example:

The Code is as follows:


Var iNum1 = 25; // 25 is equal to 0000 0000 0000 0000 0000 0000 0001
Var iNum2 = ~ INum1; // conver to 1111 1111 1111 1111 1111 1111 1110 0110
Console. log (iNum2); // outpus-26

// The bitwise operator NOT is essentially negative for the number and then minus 1, so the following code can get the same effect
Var iNum3 = 25;
Varinum4 =-iNum3-1;
Console. log (iNum4 );


2. bitwise operation AND: Expressed by (&), the binary form of the number is calculated directly. The rule is set to 1 to 1. Otherwise, it is set to 0. Example:

The Code is as follows:


Var iNum1 = 25; // 25 is equal to 0000 0000 0000 0000 0000 0000 0001
Var iNum2 = iNum1 & 3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011
Console. log (iNum2); // and is 0000 0000 0000 0000 0000 0000 0000 0001 outpus 1


3. bitwise operation OR: Expressed by (|), it directly computes the binary form of the number. The rule is 0 to 0, otherwise it is 1. Example:

The Code is as follows:


Var iNum1 = 25; // 25 is equal to 0000 0000 0000 0000 0000 0000 0001
Var iNum2 = iNum1 | 3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011


4. Bit operation (XOR): Expressed by (^), the binary form of the number is calculated directly. The rule is that when only one digit is saved as 1, it is 1. Otherwise, it is 0. Example:

The Code is as follows:


Var iNum1 = 25; // 25 is equal to 0000 0000 0000 0000 0000 0000 0001
Var iNum2 = iNum1 ^ 3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011
Console. log (iNum2); // xor is 0000 0000 0000 0000 0000 0000 0001 1010 outpus 26


5. Left shift operation: in the form of (<), all digits in the number are moved to the specified number on the left to retain the symbol bit. Moving one digit to the left is equivalent to multiplying 2.
6. shifts the right of a number by (>). This operation moves all digits of a number to the right and retains the number of signs. Moving one digit to the right is equal to dividing by 2.
7. unsigned right shift operation: It is represented by (>>>) to move all digits in the number to the right. A positive number is exactly the same as a signed right shift, and a negative number is treated as a positive number.
Example:

The Code is as follows:


Var iOld = 2;
Var iOld1 = 64;
Var iOld2 = 64;
Var iOld3 =-2;
Var iNew = iOld <5;
Var iNew1 = iOld1> 5;
Var iNew2 = iOld2 >>> 5;
Var iNew3 = iOld3 >>> 1;
Console. log (iNew); // outpus 64
Console. log (iNew1); // outpus 2
Console. log (iNew2); // outpus 2
Console. log (iNew3); /// outpus 2147483647


The negative iOld3 unsigned right shift calculation method is as follows:
First, convert-2 to an equivalent form of unsigned, that is, the binary complement of-2:
-2 non-negative binary representation: 0000 0000 0000 0000 0000 0000 0000 0010
The binary code: 1111 1111 1111 1111 1111 1111 1111 1101
Add 1111 1111 1111 1111 1111 1111 1110
Finally, move one digit to the right: 0111 1111 1111 1111 1111 1111 1111: 1111
2.9.3 Boolean operator
1. Logic NOT: (!) Indicates that the returned value must be a Boolean value. The action is as follows:
Returns false if the number of operations is an object.
True is returned if the number of operations is 0.
If the number of operations is any number other than 0, false is returned.
Returns true if the number of operations is null.
Returns true if the number of operations is NaN.
An error occurs if the number of operations is undefined.
2. Logic AND: Expressed by (&). If the operation count is Boolean, true is returned only when the operation count is true. Otherwise, false is returned. The AND operation can be of any type, AND the return value is not necessarily a Boolean value:
If one operation is an object and the other is a Boolean value, this object is returned.
If both operations are objects, the second object is returned.
If the number of operations is null, null is returned.
If the number of operations is NaN, NaN is returned.
An error occurs if the number of operations is undefined.
Logic AND in ECMAScript is also a simple operation. If the first operation determines the result, the second operation is no longer calculated. For example:

The Code is as follows:


Var bFalse = false;
Var bResult = bFalse & bUnknow;
Console. log (bResult); // outpus false
Var bTrue = true;
Var bResult = bTrue & bUnknow;
Console. log (bResult); // outpus ReferenceError: bUnknow is not defined


3. Logical OR operator: Expressed by (|). If the operation count is Boolean, false is returned only when the operation count is false. Otherwise, true is returned. The OR operation can be of any type, and the return value is not necessarily a Boolean value:
If one operation is an object and the other is a Boolean value, this object is returned.
If both operations are objects, the first object is returned.
If the number of operations is null, null is returned.
If the number of operations is NaN, NaN is returned.
An error occurs if the number of operations is undefined.
The logic OR in ECMAScript is also a simple operation. If the first operation determines the result, the second operation is no longer calculated. For example:

The Code is as follows:


Var bTrue = true;
Var bResult = bTrue | bUnknow;
Console. log (bResult); // outpus true
Var bFalse = false;
Var bResult = bFalse | bUnknow;
Console. log (bResult); // outpus ReferenceError: bUnknow is not defined


2.9.4 multiplication Operator
1. Multiplication OPERATOR: represented by (*). It is normally the same as multiplication in mathematics. In special cases, there are some special values:
If the calculation result is too large or too small, the generated result is Infinity or-Infinity.
If the number of an operation is NaN, the result is NaN.
Infinity is multiplied by 0 and the result is NaN.
Infinity is multiplied by any number other than 0, and the result is Infinity or-Infinity, determined by the symbol of the second number of operations.
Infinity is multiplied by Infinity, and the result is Infinity.
2. Division OPERATOR: Expressed by (/). Normally, it is the same as multiplication in mathematics. In special cases, there are some special values:
If the calculation result is too large or too small, the generated result is Infinity or-Infinity.
If the number of an operation is NaN, the result is NaN.
Infinity is divided by Infinity, and the result is NaN.
Infinity is divided by any number and the result is Infinity.
0 is divided by a non-infinite number and the result is NaN.
Infinity is divided by any number other than 0, and the result is Infinity or-Infinity, determined by the symbol of the second number of operations.
3. modulo operator: Expressed by (%). Normally, it is the same as multiplication in mathematics. In special cases, there are some special values:
If the divisor is Infinity or the divisor is 0, the result is NaN.
Infinity is divided by Infinity, and the result is NaN.
If the divisor is an infinite number, the result is the divisor.
If the divisor is 0, the result is 0.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.