Notice:
1. Since you are currently studying JavaScript technology, here we take JavaScript as an example. You can try it in PHP yourself.
2. JavaScript syntax is relatively complicated, so take JavaScript as an example.
I recently read the book Authoritative JavaScript Guide, which I should say is very serious about reading, so I wanted to record more of what I learned. back
I will gradually write more articles about this book.
The theoretical knowledge in this article comes from the authoritative JavaScript guide. I will organize it here, or call it notes.
If your foundation is good enough, it will not be a problem to understand it completely, but if you read it a little depressed, you can add my QQ: 76863715
The premise of reading this article is that you can distinguish what expressions are and what statements are. There are also clear what operators and operators are mostly. Place
The term expression is a JavaScript "phrase", which the JavaScript interpreter can calculate to generate a value. Expressions can
It is divided into the following three types:
1) Direct quantity, such as 1.7 is a direct quantity of numbers, "JavaScript authoritative guide" is a direct quantity of string, etc.
2) Variables
The value of the direct quantity expression is the direct quantity itself, and the value of the variable expression is the value stored or referenced by the variable.
3) You can "merge" the "simple" expressions mentioned above to create them as relatively complex expressions. For example, 1.7 is an expression, i is also an expression, and the following code shows the same (can also be called) expression:
i+1.7
The value of the above expression is the sum 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 operations
Operators can be classified according to the number of operations required by the operator. Most operators are binary operators, which combine two "expressions" into one complex expression. In short, it has two arithmetic numbers. In addition, JavaScript supports a large number of unary operators, which can convert one expression into another more complex expression. For example, in expression -3, the operator "-" is a unary operator, and it performs an operation to inverse the operation number.
JavaScript also supports the ternary operator "?:", which can merge three expressions into one complex expression.
OK, let’s start explaining the comma operator.
The comma operator, which will first calculate the parameter on the left and then calculate the parameter value on the right. Then return the value of the rightmost parameter.
The examples given in the original book are not very good and cannot explain the above sentence. Here is another one:
<script>var a = 10, b = 20;function CommaTest(){return a++, b++, 10;}var c = CommaTest();alert(a); // Return 11alert(b); // Return 21alert(c); // Return 10</script>The value of the variable c is the value returned by the function CommaTest, and a and b add 1.
Conflict between comma operator and function call operator
In JavaScript, function calls are indeed function call operators. It is very special because there is never this name in other programming language materials. Then, (that is) it does not have a fixed number of operations.
The first parameter of the function call operator is a function name or an expression that references the function, followed by brackets (). The number of operators in the middle of brackets can be varied, and these operands can be any expression separated by commas.
The function call operator will calculate each of its operations, the first operation is specified as the function name (before brackets), and the values of all operations in the middle of the brackets will be passed to this function as a parameter to the function.
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])After knowing that 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 explained according to the principle of the comma operator, it should output 8. Why?
Because the comma operator is the lowest priority in JavaScript, it is very useful to remember this. So the function call operator will run before the comma operator. The result alert function outputs the value of the first parameter. Change the above code to the following.
<script>alert((2*5, 2*4)); // Return to 8</script>
Conflict between comma operator and assignment operation
In JavaScript, the comma operator has a better priority than the assignment operator. Please see the code below.
<script>var a = 20;var b = ++a,10;alert(b);</script>
This code doesn't seem to run, probably because the assignment operator runs precedence over the comma expression, if the code is changed to
<script>var a = 20;var b = (++a,10);alert(b);</script>
Just do it.
Let’s explain the “possible” mentioned above here. These are some of my views and may not be authoritative.
The comma operator requires that its operand is a complex expression or a simple expression (such as a variable or a direct quantity), but since the assignment operator takes precedence over the comma operator, it becomes not an operand or an expression on the left, but a statement containing the var keyword
Code that could not be executed before can be regarded as the following code:
<script>var a = 20;(var b = ++a),10;alert(b);</script>
There are expression statements in the statement, but not all statements are expressions.
#######################################################################################
1. Characteristics and functions of comma operators
The comma operator is to concatenate several expressions. Its priority is the lowest among all operators, and the binding direction is "left to right".
For example: 3*3, 4*4
2. Comma expression
The general form of comma expression is: expression 1, expression 2, expression 3... expression n
The solution process of comma expression is: first calculate the value of expression 1, then calculate the value of expression 2, and... until the value of expression n. Finally, the value of the entire comma expression is the value of the expression n.
See the following examples:
x=8*2,x*4 /*The value of the entire expression is 64, and the value of x is 16*/
(x=8*2,x*4),x*2 /*The value of the entire expression is 128, and the value of x is 16*/
x=(z=5,5*2) /*The entire expression is an assignment expression, its value is 10, and the value of z is 5*/
x=z=5,5*2 /*The entire expression is a comma expression, its value is 10, and the values of x and z are 5*/
There are not many places to use comma expressions, and they are usually only used when assigning initial values to loop variables. Therefore, not all commas in a program should be regarded as comma operators, especially when calling a function, each parameter is separated by a comma, and the comma is not a comma operator.
For example: printf(" %d,%d,%d",x,y,z);
############################################################################################
The operator causes the expressions on both sides to be executed in order from left to right, and obtains the value of the expression on the right. , The most common use of operators is to be used in incremental expressions of for loops. For example:
for (i = 0; i < 10; i++, j++){k = i + j;}Each time passing through the end of the loop, the for statement allows only a single expression to be executed. , operators are used to allow multiple expressions to be treated as a single expression, thereby circumventing this limitation.
The above article briefly discusses the usage of comma operators in JS is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.