Order of the javascript operation count

Source: Internet
Author: User

For example Copy codeThe Code is as follows: a * B + c;

First calculate the Multiplication Side, then calculate the multiplication and division, and finally calculate the addition and subtraction
, With parentheses. First, calculate the values in the brackets and perform the same-level operations in sequence from left to right.
All programming languages use the computational 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 in which the number is evaluated?
As follows:Copy codeThe Code is as follows: // calculate the sum of a and B
Sum = a + B;

, Get the value of a from the memory
, Takes the value of B from the memory.
To perform the addition operation.
It seems that the description is very mentally retarded. Of course it is. Some people may think that we should first take the value of B, then a, and then add. The final result is the same. This is indeed the case. But what if the number of operations is a function execution?
Sum = a + fun ();
Assume that only one number is returned in the fun function. In this case, it doesn't matter whether the value of a is obtained first or the value after fun execution. The final results are the same. Here, there is still nothing new and tangled.
But what if fun not only returns numbers but also changes? For example, the following JavaScript codeCopy codeCode: 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 is a involved in the addition operation. In this case, do a get 5 or 10 to participate in the addition operation? If it is 5, the value of B is 25. If it is 10, B is 30. In JavaScript, the result is 25. But the C language is 30, as shown below:Copy codeThe Code is as follows: int a = 5;
Int fun (){
A = 10;
Return 20;
}
Int B = a + fun (); // 30

Therefore, when a function has a side effect, the order of calculation numbers is different, resulting in different results. Obviously, different languages are implemented.
In JavaScript, a is 5 from left to right. After fun is executed, 20 is returned, and 5 + 20 is returned. Note that although a takes 5 for this operation, the value of a has actually changed. As follows:Copy codeCode: var a = 5;
Function fun (){
A = 10;
Return 20;
}
Var B = a + fun (); // The value of B?
Alert (a); // 10

In C language, perform fun first, and change the value of a in fun to 10. Take a as 10 to participate in this "add" operation. fun returns 20. The result is 10 + 20.
As you can see, whether it is JavaScript or C. The value of a is changed to 10. The difference is: when involved in the addition operation, JavaScript takes the unaltered value 5, and C takes the changed value 10.
In C, the operation is from left to right. However, when a function is used as the number of operations, the function is executed first. If the function has any side effects, the value a after the change participates in this operation. Regardless of the order of fun and. Put fun in the front, and the result in C language is still 30Copy codeThe Code is as follows: int a = 5;
Int fun (){
A = 10;
Return 20;
}
Int B = fun () + a; // 30

In JavaScript, if fun and a Exchange Order, the result is not 25.Copy codeCode: var a = 5;
Function fun (){
A = 10;
Return 20;
}
Var B = fun () + a; // B is 30

Related:
Function side effects

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.