The use of the JS comma operator is detailed

Source: Internet
Author: User

Explanation of the use of the comma operator

Attention:
First, because of the current reading JavaScript technology, so here take JavaScript as an example. You can try it yourself in PHP.
Second, JavaScript syntax is more complex, so take JavaScript as an example.


The most recent re-reading of the JavaScript authoritative guide is that it should be read very carefully, so I want to record more of what we have learned. After

I will gradually write more articles about this book.

The theoretical knowledge of this article comes from the JavaScript authoritative guide, which I do here to sort out, or call notes.

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


The premise of reading this article is that you can distinguish between what is an expression and what is a statement. There are also explicit operators and operands that are more of something. The

The expression is a "phrase" of JavaScript, which can be computed by a JavaScript interpreter, thus generating a value. An expression can be

To be divided into the following three kinds:

1) Direct volume, such as 1.7 is the direct amount of digital, "JavaScript authoritative guide" is a string of direct amount and so on.
2) variables

The value of the direct amount expression is the direct quantity itself, and the value of the variable expression is the value that the variable is holding or referencing.

3) You can "merge" the "simple" expression mentioned above to create an expression that is more complex. For example, 1.7 is an expression, I is an expression, and the following code shows the same (or can be called) an expression:
i + 1.7
The value of the expression above is the and of two simple expressions (a variable expression and a simple expression). In this example, "+" is an operator that combines two simple expressions to form a complex expression.

Number of operands
Operators can be categorized according to the number of operands required by the operator, and most operators are two-tuple operators, which combine two "expressions" into a complex expression. In a nutshell, it has two operands. In addition, JavaScript supports a large number of unary operators, which convert one expression to another more complex expression. As in Expression 3, the operator "-" is the unary operator, and it performs an operation on the operand counter.
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 evaluates the left argument first, and then calculates the value of the right parameter. The value of the rightmost parameter is then returned.
The original book example is not very good, unable to explain the above sentence, here is another:

<script>

var a = ten, B = 20;

function Commatest () {
Return a++, b++, 10;
}

var c = commatest ();

alert (a); Returns 11
alert (b); Returns 21
alert (c); Returns 10

</script>

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

Conflicts between the comma operator and the function call operator
In JavaScript, a function call is really a function call operator. It's very special, because many other programming language materials never have that term. Then, it doesn't have a fixed number of operands.

The first parameter of the function call operator is a function name or an expression that refers to a function, followed by parentheses (). The middle of the parentheses 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 operand is specified as the function name (before the parentheses), and the value of all operands in the middle of the parenthesis is passed to the function as a function parameter.
For example:

Document.close ()
Math.sin (x)
Alert ("Welcome" + name)
DATE.UTC (2000, 11, 31, 23, 59, 59)
FUNCS.F (Funcs.args[0], funcs.args[1])

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

<script>
Alert (2*5, 2*4); Output 10
</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 is it?

It is useful to keep in mind that the comma operator is at the very bottom of the JavaScript precedence. So the function call operator runs before the comma operator. The result of the alert function outputs the value of the first parameter. Modify the above code to look like the following.

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


Conflicts assigned by the comma operator and assignment operation

In JavaScript, the precedence of the comma operator is more than the value of the assignment operator. Take a look at the following code.

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

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

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

You're ready.



The above mentioned "may" We here to explain, this is some of my views, not necessarily authority.

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

Code that cannot be executed before can be considered as follows:

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

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

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

The characteristics and functions of the comma operator
The function of the comma operator is to concatenate several expressions. Its priority level is the lowest in all operators, with the direction of "from left to right".
such as: 3*3,4*4
Two, comma expression
The general form of a comma expression is: expression 1, expression 2, expression 3 ... Expression n
The solution to the comma expression is to evaluate the value of the expression 1, then the value of the expression 2, ... Always evaluates to the value of the expression N. The value of the last entire comma expression is the value of the expression N.
Look at the following examples:
X=8*2,X*4/* The value of the entire expression is 64,x and the value is 16*/
(x=8*2,x*4), x*2/* The value of 128,x for the entire expression is 16*/
x= (z=5,5*2)/* The entire expression is an assignment expression with a value of 10,z of 5*/
X=Z=5,5*2/* The entire expression is a comma expression whose value is 10,x and Z are 5*/
The comma expression is not used in many places, and is usually used only when assigning an initial value to a loop variable. So not all commas in the program are treated as comma operators, especially when the function is called, each parameter is separated by commas, and the comma is not a comma operator.
such as: printf ("%d,%d,%d", X, Y, z); The ########################################################### operator causes the expression on either side of it to be executed from left to right, and to get the value of the right-hand expression. , the most common purpose of an operator is to use it 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 allow multiple expressions to be treated as a single expression, thereby circumventing the restriction.

The use of the JS comma operator is detailed

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.