JavaScript Advanced Programming Reading notes (v) operators in ECMAScript (i) _javascript tips

Source: Internet
Author: User
Tags bitwise object object
2.9 operator
2.9.1, unary operators
The unary operator has only one parameter, the object or value to manipulate.

1. Delete: Deletes a reference to a previously defined object property or method, but this operator cannot remove properties and methods that are not defined by the developer. Example:
Copy Code code as follows:

var o=new Object;
O.name= "Nicholas";
O.show=function () {
return "Test";
};
Console.log (O.name); Outpus Nicholas
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: Returns undefined for any value, which is typically used to avoid values that the output should not output.

If you open a new window in the link, the code is as follows:

<a href= "Javascript:window.open (' About:blank ')" >click me</a>
Clicking on the link will appear in the new Window (object). 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 invoke the window.open () function with the void operator:

<a href= "javascript:void (window.open (' About:blank '))" >click me</a>
3, forward increment/front decrement operator: Two operators borrowed from C. Example:
Copy Code code as follows:

var inum=10;
Console.log (++inum); Outpus Same as Inum=inum+1
Console.log (Inum); Outpus 11
Console.log (--inum); Outpus Same as Inum=inum-1
Console.log (Inum); Outpus 10

4, post increment/after decrement operator: Two operators borrowed from C. As with the previous increment/decrement, the value is added by 1 or minus 1, unlike the postfix operators that increment or decrement operations after the expression that contains them is computed. Example:
Copy Code code 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-element addition and one-element subtraction: usage is the same as that of high school mathematics, one-element addition has no effect on the number, and one-element subtraction is the negation of the value. However, the unary and unary subtraction are similar to parseint () in the case of a string, and the main difference is that only a string that begins with "Ox" is converted to a value of 10. Example:
Copy Code code 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 operator
1, bitwise operation not: by (~), the processing process is as follows:

(1) Convert the OP count to 32 digits

(2) converting the binary form into its binary inverse code;

(3) Convert binary inverse code to floating-point number

Example:

Copy Code code as follows:

var inum1=25; equal to 0000 0000 0000 0000 0000 0000 0001 1001
var inum2=~inum1; Conver to 1111 1111 1111 1111 1111 1111 1110 0110
Console.log (INUM2); Outpus-26

The bitwise operator is not essentially negative for a number and then minus 1, so the following code can get the same effect
var inum3=25;
var inum4=-inum3-1;
Console.log (INUM4);

2, bitwise operations and: by (&), directly to the binary form of the number of calculations. The rule is 1 for 1, or 0 for the total. Example:
Copy Code code as follows:

var inum1=25; equal to 0000 0000 0000 0000 0000 0000 0001 1001
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: by (|) Represents a direct calculation of the binary form of a number. The rule is 0 for 0, or 1 for the total. Example:
Copy Code code as follows:

var inum1=25; equal to 0000 0000 0000 0000 0000 0000 0001 1001
var inum2=inum1|3; 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011

4, bitwise Operation XOR: by (^), directly to the binary form of the number of calculations. The rule is that only one digit is stored at 1 o'clock to 1, or 0. Example:
Copy Code code as follows:

var inum1=25; equal to 0000 0000 0000 0000 0000 0000 0001 1001
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: by (<<), all digits in the number to the left to move the specified number of digits, keep the symbol bit, the left one is equal to multiply by 2.
6, signed right shift operation: by (>>), all digits in the number to the right to move the specified number of digits, keep the symbol bit, the right to move one equivalent divided by 2.
7, unsigned Right shift operation: by (>>>), all digits in the number to the right to move the specified number. For positive numbers, the same as the signed right shift, the negative numbers are treated as positive numbers.
Example:
Copy Code code 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

Negative iOld3 The unsigned right shift is calculated as follows:
First, convert 2 into an unsigned equivalent form, that is, a binary complement of 2:
-2 nonnegative version binary: 0000 0000 0000 0000 0000 0000 0000 0010
The binary counter code: 1111 1111 1111 1111 1111 1111 1111 1101
Add 1:1111 1111 1111 1111 1111 1111 1111 1110 on the binary counter code
Finally move to the right one: 0111 1111 1111 1111 1111 1111 1111 1111 is: 2147483647
2.9.3 Boolean operator
1, logical not: by (!) Indicates that the return value must be a Boolean value and behaves as follows:
Returns False if the OP count is an object
If the OP count is number 0, return True
If the OP count is any number other than 0, return false
Returns True if the OP count is null
Returns True if the OP count is Nan
If the OP count is undefined, there's a mistake.
2, Logical AND: by (&&), if the operands are all Boolean, returns true only if the operands are true, or false. The operand of the and operation can be of any type and the return value is not necessarily a Boolean value:
If one operand is an object and the other is a Boolean value, return the object
If two operands are objects, the second object is returned
If a shipment count is NULL, return null
If a shipping count is Nan, return nan
If an op count is undefined, an error occurs.
The logical and is also a simple operation in ECMAScript, that is, if the first operand determines the result, the second operand is no longer computed, and the example:
Copy Code code 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: by (| |) Indicates that if the operands are all Boolean, False is returned only if the operands are false, otherwise true. The operand of an OR operation can be of any type and the return value is not necessarily a Boolean value:
If one operand is an object and the other is a Boolean value, return the object
If two operands are objects, the first object is returned
If a shipment count is NULL, return null
If a shipping count is Nan, return nan
If an op count is undefined, an error occurs.
The logical or in the ECMAScript is also a simple operation, that is, if the first operand determines the result, the second operand is no longer computed, and the example:
Copy Code code 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: by (*), is normally the same as the multiplication in mathematics, special cases some special values:
If the result of the operation is too large or too small, the resulting result is Infinity or-infinity
If a shipping count is Nan, the result is Nan
Infinity Times 0, and the result is Nan
Infinity times any number other than 0, the result is Infinity or-infinity, determined by the symbol of the second operand
Infinity times Infinity, the result is infinity
2, Division Operator: by (/), normally with the math in the same multiplication, special cases some special values:
If the result of the operation is too large or too small, the resulting result is Infinity or-infinity
If a shipping count is Nan, the result is Nan
Infinity is infinity and the result is Nan
Infinity is removed by any number and the result is infinity
0 divided by a number that is not infinitely large, the result is Nan
Infinity by any number other than 0, the result is Infinity or-infinity, determined by the symbol of the second operand
3, modulo operator: by (%), is normally the same as the multiplication in mathematics, special circumstances, some special values:
If the divisor is infinity, or the divisor is 0, the result is Nan
Infinity is infinity and the result is Nan
If the divisor is infinitely large, the result is dividend
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.