The grammar of the essence of JavaScript language (1) supplements

Source: Internet
Author: User
Tags arithmetic

Logical operations

Two kinds of logical operations are supported in JavaScript, "Logical OR (| | and logic and (&&), their use is consistent with the basic Boolean operation:

var str= ' Hello ';
var obj = {};
x = str | | Obj
y = str && obj;

What is special about this operator is that he does not change the data type of the operand, nor does it enforce the data type of the result of the operation. In addition, there are two features:

    • Operator will interpret the operator as a Boolean value;
    • The operation process supports a Boolean short circuit.

So in the example above,

The result of x operation is: STR;

The result of the Y operation is: obj.

comparison Operation

1. Equivalence detection

The purpose of equivalence detection is to determine whether two variables are equal or identical.

Equality refers to the operator "= =" and "! =", the same as the operator "= = =" and "!==".

Compare two expressions to see if they are not equal

Compares two expressions to see if they have unequal values or different data types.

TD valign= "Top" width= "410" >

name

operator

Description

Equality

= =

Compares two expressions to see if they are equal.

Unequal

! =

same

= = =

Compare two expressions to see if the values are equal and have the same data type.

different

!==

Arithmetic rules of "equality" in equivalence detection

Type

Arithmetic rules

Two value types for comparison

Values that are converted to the same data type are compared with "data equivalence".

Value types are compared to reference types

Converts the data of the reference type into the same data as the value type data, and then compares the data equivalents.

Two comparison of reference types

Compare the reference address.

"Same" arithmetic rules in equivalence detection

Type

Arithmetic rules

Two value types for comparison

If the data type is different, it is necessarily "not the same"; The data type is the same as the "numerical equivalence" comparison.

Value types are compared to reference types

Must be "different".

Two comparison of reference types

Compare the reference address.

In particular, for example, the equivalent comparison of reference types, the practical meaning of "compare reference addresses" is that if it is not the same variable or its reference, the two variables are not equal.

var str = ' abcdef ';
var obj1 = new String (str);
var obj2 = new String (str);

alert (obj1==obj2);//return False
alert (obj1===obj2);//return False

"= = =" and "! General purpose of the = = operator

In JavaScript, false, ' ', 0, and null, undefined are 22 "equal":

Alert (' ==0 ');
alert (0==false);
Alert (false== ');
alert (undefined==null);

The results above are true, so you should use "= = =" and "!==" when comparing the data elements that contain the above 5 cases.

PS: A value should be "equal/identical" to itself, but there is an exception: a Nan value that is not equal to itself and is not the same.

2. Sequence comparison (compare size)

Types that can compare sequences

Sequence value

Boolean

0~1

String

Number

Negative Infinity ~ Positive infinity

Type

Arithmetic rules

Two value types for comparison

Directly compares the size of the data in the sequence.

Value types are compared to reference types

Converts the data of a reference type to the same data as the value type data, and then a "sequence size" comparison.

Two comparison of reference types

No meaning. (* Note 1)

Note 1: In fact, sequence detection operations on reference types are still possible, but this is related to the effect of the valueof () operation.

Here are the examples above:

Comparison of value types, Boolean values and numeric values
var b0 = false;
var B1 = true;
var num = 1;
alert (b1<num);//false
alert (b1<=num);//true
alert (b1>b0);//true

Value types and reference types
var num = 1;
var ref = new String ();
alert (num > ref);//true

Comparison of reference types
var o1={};
var o2={};
Alert (O1 > O2 | | O1 < O2 | | o1 = o2);//false

Two operands are strings, comparing the size of each character in a string
var S1 = "abc";
var s2 = "AB";
Alert (S1 > s2);//true

Converts a string to a numeric comparison when the string is compared to another type value
var s3 = "101";
var i= "100";
Alert (S3 > I); true

When a string is converted to a numeric value, Nan is obtained, and any data is compared to Nan as false
var s4 = "ABC";
var i = "100";
Alert (S4 > I);

void operator

The syntax format for using void is: [void expression].

The effect is to make the subsequent expression execute immediately, but ignore the return value (return undefined).

The following example is a good illustration of the meaning of this sentence:

var S1 = "1";
function Testvoid ()
{
S1 = "2";
}
var s2 = void testvoid ();
alert (s1);//2
alert (s2);//undefined

With this feature, we can make the href attribute in the <a> tag lose its function, as we often see:

The parentheses after void are not necessary, except that it still satisfies the syntax format of [void expression], which is equal to (0), and should never be considered a function call.

It should be possible to think that since the use of void to ignore the return value of the feature, then the link does not have to perform the default behavior is not what we thought 0 in effect, the following form will have the same effect:

<a href= "Javascript:void 1" >i am a useless link</a>
<a href= "javascript:void ' ABCDE '" >i am a useless link</a>
<a href= "javascript:void (1 + 2)" >i am a useless link</a> <!--There's no parentheses here.

function Call

The most common method of function invocation:

Named function calls directly
function foo ()
{

}
Foo ();

Anonymous functions are invoked by reference.
var fooref = function () {}
Fooref ();

Here's a look at some of the less common ways to call functions:

anonymous function call with no reference 1
(//Here the parentheses represent the force operator
function ()
{
Alert (' Test ');
} ()//The parentheses here represent the function call operator (* note)
);
After execution, the pop-up ' test ' indicates that the function executed successfully.
The following is an analysis of the code, first of all the internal functions,
function ()
{
Alert (' Test ');
}()
This is not clear enough, this code is equivalent to
function Fun1 ()
{
Alert (' Test ');
}()
If you don't think it's clear enough, it's still equivalent to
function Fun1 ()
{
Alert (' Test ');
};
();
That is, the function declaration before the 1th semicolon is interpreted as a complete grammatical structure, while the following "();" is obviously a wrong syntax,
So what makes the above function call a successful execution? The answer is the outermost enforcement operator.
We know that the force operator is to force an operand that would not have an operational relationship, such as: 1*2+3*4, does not operate in accordance with the logic of 2+3, but 1* (2+3) can.
Back to the above example, the outermost enforcement operator enforces the two-segment statement that should be interpreted separately, and the effect is to produce a function call!

anonymous function call with no reference 2
(
function ()
{
}
) ();//The parentheses here represent the function call operator (* note)
After the above example, this is a better understanding.
The force operator here is the expression "function direct volume Declaration" and returns a reference to a function, and then the function call operator "()" to manipulate the function reference.

anonymous function call with no reference 3
void function ()
{

} ();//The parentheses here represent the function call operator (* note)
operator void is used to perform operations on subsequent function expressions, which can be understood as a force operation, similar to Example 1.

The above 3 examples (* note) The place is the function call operator, since it is the syntax of the function call, and normal function calls, it is also possible to pass parameters.

void function (s)
{
alert (s);
} (' Test ');

Uncommon usage of break

In general, the break clause acts on the inner or switch statement of a loop statement, but the break clause also has an extended syntax to indicate the scope it is scoped to.

The scope is represented by a label that has already been declared:

Break Some_label;

Some_label:
{
var S1 = "1";

if (S1 = = "1")
{
Break Some_label;
}
S1 = "2";

}
alert (S1);//The result is 1, indicating that S1 = "2" is not executed.

uncommon usage of continue

Continue can also be followed by a label, which indicates that it is aborted from inside the loop body and continues to execute at the label instruction.

Some_label:
for (var i = 1; I <3;i++)
{
For (var k =11; k < 13;k++)
{
if (k==12)
{
Continue Some_label;
}
Alert ("i =" + i + "; k =" + K ");
}
}
Eject i = 1;k = one i = 2;k = 11
Directly see the code is not easy to understand, you can add a breakpoint debugging, in fact, here the meaning of the continue some_label from the internal loop,
and continues the next loop of the outer, the normal continue is the next loop that executes the current loop.
It is important to note that you cannot add curly braces after the continue corresponding label. There are no goto statements in JavaScript, and the break and continue statements provide the possibility of another choice.

The ambiguity of the operator

One, plus "+" of the Ambiguity

The plus sign as an operator has three functions in javascript:

1. String connector

2. Expression summation operator

3. Numeric Positive operators

The 3rd is not used, he means a positive sign in mathematics.

var n = 1;
Alert (+n),//= + (1), the result is still 1. Can be compared with alert (-N), where the result is-1

Of these three cases, 1, 22 points are the most prone to two of the semantic problem. Because these two processing operations are dependent on the data type, they cannot be judged from the operator. Such as:

c = a + B;

It is impossible to know what his true meaning is in summing or in making a string connection.

Due to the existence of this ambiguity there will be some problems. In the browser, because many of the DOM model's values appear to be numbers, they are actually strings. So when trying to do an addition, it actually becomes a string connection.


<script>
Alert (TestImg.style.borderWidth +);//1px10
</script>

In addition, there is a rule about the plus operator: If a string exists in the expression, the operation is prioritized by the string connection.

Alert ("123" +456);//123456
Alert (123+ "456");//123456

Second, the comma "," the Ambiguity

Let's look at the following examples to analyze the meaning of their expressions:

Example 1
A= (a);

Example 2
a=1,2,3;

Example 3
a=[1,2, (3,4,5), 6];

Example 4
var a = n/A;

In Example 1, a comma is used as a continuous operator, and the meaning of a continuous operator (also known as a comma expression) is to return the value of the last expression, and the effect of Example 1 is "variable A is assigned a value of 3".

Example 2 The entire expression is a comma expression, so the value of the entire expression is the value of the last expression 3, and variable A is assigned a value of 1.

The comma in Example 3 has two meanings (3,4,5) in which the comma represents a continuous operator, while in the middle represents the syntax delimiter at the time of the array declaration, so example 3 is an array that declares a [1,2,5,6].

Example 4 does not perform as normal as Example 2. Because the comma here is interpreted as the syntax delimiter used to separate multiple variables (var a, b,c) in the statement Var declaration, and the number 2,3 is an illegitimate syntax declaration, an error is prompted during the interpretation period.

Ambiguity of the square brackets "[]"

Example 1
A = [[1][1]];

This strange statement has no grammatical errors, although we can hardly understand it, but the JavaScript interpreter is understandable. In order to understand this problem, it is necessary to know the role of square brackets:

1. Used to declare the direct amount of an array.

2. Access the array subscript operator.

3. Object member access.

So the first "[1]" is understood as the direct amount of an array, and it has only one element. Next, because he is the object, the second "[1]" is understood to take down the element labeled 1, it is clear that this element has not yet been declared.

Thus the result of "[1][1]" is undefined, and a becomes a =[undefined], and only an array of elements, which is undefined.

Let's look at a slightly more complicated example:

var a = [[' A ', 1,2][' B ', 3,4]];

[' A ', up] is still a direct amount of an array, but [' B ', 3,4] how to explain it?

The first thing to know about ' B ' is that 3,4 is interpreted as a comma expression, and his value is 4, so [4] represents the access of the array member, and the following procedure is the same as the previous example.

The essence of JavaScript language (1) syntax summary supplements (GO)

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.