On the usage _javascript technique of the comma operator in JS

Source: Internet
Author: User

Attention:

First, as the current reading of JavaScript technology, so here take JavaScript as an example. You can try it in PHP yourself.

Second, JavaScript syntax is more complex, so take JavaScript as an example.

The recent re-reading of the JavaScript Authority guide, which should be read very carefully, has meant to record more of what you have learned. After

I will gradually write more articles about this book.

The theoretical knowledge of this article comes from the JavaScript Authority guide, where I do some sorting, or call notes.

If your foundation is good enough, you can fully understand the problem, but if you read a little depressed, you may add my qq:76863715

The premise of reading this article is that you can differentiate between what is an expression and what is a statement. There is also the explicit operator and the number of operands is something. The

The predicate expression is--a JavaScript "phrase" that the JavaScript interpreter can compute to generate a value. An expression can be

To be divided into the following three kinds:

1 The direct amount, such as 1.7 is the number direct quantity, "JavaScript Authority Guide" is a string direct quantity and so on.

2) variable

The value of a direct-weight expression is the direct amount itself, and the value of the variable expression is the value that the variable holds or references.

3 can be "merged" with the "simple" expression mentioned above to create a more complex expression. For example, 1.7 is an expression, I is an expression, and the following code is also (or can be called) an expression:

i + 1.7

The value of this expression above is the and of two simple expressions (a variable expression and a simple expression). In this example, "+" is an operator used to combine two simple expressions to form a complex expression.

Number of operands

Operators can be categorized based on the number of operands required by the operator, most of which are two-dollar operators, which combine two expressions into a complex expression. In short, it has two counts. In addition, JavaScript supports a large number of unary operators, which can transform one expression into another more complex expression. As in expression-3, the operator "-" is the unary operator, which performs the inverse of the operand.

JavaScript also supports ternary operators "? : "It can combine three expressions into a complex expression."

OK, let's start with the comma operator.

The comma operator, which computes the left argument first, and then calculates the parameter value on the right. The value of the rightmost argument is then returned.

The example given in the original book is not very good enough to explain the above sentence, here is another one:

<script>
var a = ten, B =;

function Commatest () {return
a++, b++;
}

var c = commatest ();

alert (a); Return one
alert (b);//return
alert (c);//Back to

</script>

The value of the variable C is the value returned by the function commatest, while A and B are 1 more.

Conflicts between the comma operator and the function call operator

In JavaScript, a function call is indeed a function invocation operator. It is very special, because many other programming language materials have never been called this. Then, it doesn't have a fixed number of shipments.

The first parameter of a function call operator is a function name or an expression that refers to a function, followed by parentheses (). The middle bracket can be an indefinite number of operators, which can be arbitrary expressions separated by commas.

The function call operator computes each of its operands, the first is specified as the function name (before parentheses), and the value of all operands in the middle bracket is passed to the function as an argument to the function.

For example:

Document.close ()
Math.sin (x)
alert ("Welcome" + name) DATE.UTC (+)
funcs.f ( Funcs.args[0], funcs.args[1])

Once you know the calling function operators, let's give an example of how to deal with their conflicts.

<script>
alert (2*5, 2*4);//Output
</script>

The above code outputs 10, but if it is interpreted according to the principle of the comma operator, it should be output 8. Why, then?

This is useful to remember because the comma operator is at the bottom of the precedence of JavaScript. So the function call operator runs before the comma operator. The result alert function outputs the value of the first parameter. Modify the above code as shown below.

<script>
Alert ((2*5, 2*4));//return 8
</script>

Conflicts between comma operators and assignment operations

In JavaScript, the comma operator has precedence over the assignment operator. Take a look at the code below.

<script>
var a =;
var b = ++a,10;
alert (b);
</script>

This code does not seem to run, possibly because the assignment operator takes precedence over a comma expression, and if you change the code to

<script>
var a =;
var B = (++a,10);
alert (b);
</script>

can be done.

The above "possible" we do here to explain, this is my opinion, not necessarily authoritative.

The comma operator requires that its operands be a complex expression or a simple expression (such as a variable or a direct amount), but because the assignment operator takes precedence over the comma operator, the left side is not an operand or an expression, but a statement containing the VAR keyword

Code that cannot be executed before can be considered the following code:

<script>
var a =;
(var B = ++a), ten;
alert (b);
</script>

There are expression statements in the statement, but not all statements are expressions.

#################################################

The characteristics and functions of a comma operator

The function of the comma operator is to concatenate several expressions. Its precedence is lowest in all operators, and the combination direction is "from left to right".

such as: 3*3,4*4

Two, comma expression

The general form of a comma expression is an expression 1, an expression 2, an expression 3 ... Expression n

The solution to a comma expression is to first evaluate the value of expression 1 and then compute the value of expression 2 ... The value of the expression n is evaluated all the time. The last value of the entire comma expression is the value of the expression N.

Look at these examples:

X=8*2,X*4/* The value of the entire expression is 64,x value is 16*/

(x=8*2,x*4), x*2/* The value of the entire expression is 128,x value is 16*/

x= (z=5,5*2)/* The entire expression is an assignment expression, and its value is 10,z value is 5*/

X=Z=5,5*2/* The entire expression is a comma expression, and its value is 10,x and Z are 5*/

The comma expression is not used in much of the place, and is usually used when assigning an initial value to a cyclic variable. So not all commas in a program are treated as comma operators, especially when function calls, where arguments are separated by commas, and commas are not comma operators.

such as: printf ("%d,%d,%d", x,y,z);

###################################################

operator so that the expressions on both sides of it are executed in left-to-right order, and the value of the right expression is obtained. , the most common use of an operator is in an increment expression for a for loop. For example:

for (i = 0; i < i++, j + +)
{
k = i + j;
}

Each time the end of the loop is passed, the For statement allows only a single expression to be executed. , an operator is used to circumvent the restriction by allowing multiple expressions to be treated as a single expression.

Above this article on the use of the comma operator in JS is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.