Home > Web Front-end > JS Tutorial > body text

javascript operand evaluation order_javascript skills

WBOY
Release: 2016-05-16 18:03:16
Original
1162 people have browsed it

For example,

Copy code The code is as follows:

a * b c;

, calculate exponentiation first, then multiplication and division, and finally addition and subtraction
, if there are parentheses, calculate what is inside the parentheses first, and operations at the same level are performed in order from left to right
All programming at this point Languages ​​adopt the calculation order of numbers in mathematics. Of course, there are some operators in programming languages ​​that are different from those in mathematics. What is the order of evaluation of operands?
As follows
Copy the code The code is as follows:

// Find the sum of a and b
sum = a b;

, get the value of a from the memory
, get the value of b from the memory
, perform the addition operation
seems to be very descriptive Mental retardation, of course that is what it is. Some people may think that first take the value of b, then take a, and then add them. The end result is the same. Indeed. But what if the operand is a function execution?
sum = a fun();
Assume that the fun function only returns one number. At this time, it doesn't matter whether you take the value of a first and then get the value after fun is executed. The end result is the same. Speaking of which, there is still nothing new or confusing.
But what if fun not only returns a number, but also changes a? For example, the following JavaScript code
Copy code The code is as follows:

var a = 5;
function fun(){
a = 10;
return 20;
}
var b = a fun(); // The value of b?

The fun function not only returns 20, but also changes the value of a. And a is the a that participates in the addition operation. At this time, should a take 5 or 10 to participate in the addition operation? If it is 5 then the value of b is 25, if it is 10 then the value of b is 30. The result in JavaScript is 25. But in C language it is 30, as follows
Copy the code The code is as follows:

int a = 5;
int fun(){
a = 10;
return 20;
}
int b = a fun(); // 30

Therefore, only when the function has side effects, the result will be different if the order of evaluation of the operands is different. Obviously, each language implements it differently.
In JavaScript language, from left to right, a takes 5, fun returns 20 after execution, and finally 5 20. Note that although a is 5 when participating in this operation, the value of a has actually changed. As follows
Copy code The code is as follows:

var a = 5;
function fun( ){
a = 10;
return 20;
}
var b = a fun(); // value of b?
alert(a); // 10

In C language, fun is executed first, and the value of a is changed in fun, which is 10. Take a as 10 to participate in this "addition" operation , fun returns 20. The result is 10 20.
You can see, whether it is JavaScript or C. The value of a finally changed to 10. The difference is that when participating in the addition operation, JavaScript takes the unchanged value 5, and C takes the changed value 10.
In C language, operations are also from left to right. But when there is a function as an operand, the function will be executed first. If the modified function has side effects, the changed a value will be used to participate in this operation. Regardless of the order of fun and a. Put fun in front as follows, in C language the result is still 30
Copy the code The code is as follows:

int a = 5;
int fun(){
a = 10;
return 20;
}
int b = fun() a; // 30

If the order of fun and a is exchanged in JavaScript, the result will not be 25.
Copy code The code is as follows:

var a = 5;
function fun() {
a = 10;
return 20;
}
var b = fun() a; // b is 30

Related:
Side effects of functions
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template