The sequence of _javascript of JavaScript operands

Source: Internet
Author: User
Like what
Copy Code code as follows:

A * b + C;

, calculate the powers first, calculate the multiplication and division, and then add and subtract
, with parentheses, first of all parentheses, the same level of operation in order from left to right
At this point all programming languages take the computational order of numbers in mathematics. Of course, there are some operators in programming languages that are different from mathematics. What is the order of the operands?
As follows
Copy Code code as follows:

A and B's and
sum = a + b;

, take the value of a from memory
, take the value of B from memory
, the addition operation
Seems to describe the very retarded, of course, this is the case. One might think that the value of B is taken first, then a, and then added. So the final result is the same. That's true. But what if op arithmetic is a function of execution?
sum = a + fun ();
Suppose the fun function only return a number. At this time, whether it is first to take the value of a, and then to fun after the implementation of the value is irrelevant. The end result is the same. Speaking of this, there is still no fresh, tangled place.
But what if fun not only returned the numbers, but also changed a? Like the following JavaScript code
Copy Code code 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 exactly a part of the add operation. When a takes 5 or 10 to participate in the addition operation? If it is 5 then the value of B is 25, and if it is 10, B is 30. The result in the JavaScript language is 25. But the C language is 30, as follows
Copy Code code 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 calculation order of the operands will result in different values. Obviously, each language is implemented differently.
JavaScript language, from left to right, a takes 5,fun to return 20 after execution, and finally 5+20. Note that although a takes 5 when participating in this operation, a value has actually changed. As follows
Copy Code code as follows:

var a = 5;
function Fun () {
A = 10;
return 20;
}
var B = a + fun (); The value of B?
alert (a); 10

C language, the first implementation of the Fun,fun to change the value of a, 10, take a for 10 to participate in this "plus" operation, fun return 20. The result is 10+20.
As you can see, whether it's JavaScript or C. The value of a was eventually changed to 10. The difference is that when you participate in the add operation, JavaScript takes the unchanged value of 5, and C takes the changed value of 10.
In the C language, the operation is from left to right. But when a function is counted as a shipment, the function is executed first. If there are side effects of the modified function, take the changed a value to participate in this operation. Regardless of the order of fun and A. The following puts the fun in front, and the result is still 30 in C language.
Copy Code code 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 Code code as follows:

var a = 5;
function Fun () {
A = 10;
return 20;
}
var B = Fun () + A; B is 30

Related:
Side effects of functions

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.