JavaScript is not "pass by value"

Source: Internet
Author: User

JavaScript is not "pass by value"
1. Some people may question the question: "In JavaScript, the basic type is passed by value, and the reference type is also passed by value. But did you tell me that JavaScript does not pass by value?" Indeed, in JavaScript, the basic type is passed by value, but for "accuracy", the reference type is not passed by value, and of course not by reference. What is the transfer? First, give an introduction. In the JavaScript advanced programming (Third edition), the parameter transmission is described as follows. The parameters of all functions in ECMAScript are passed by value. That is to say, copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another. The transfer of the basic type value is the same as that of the basic type variable. The transfer of the reference type value is the same as that of the reference type variable. Many developers may be confused about this, because there are two ways to access variables: by value and by reference, and parameters can only be passed by value. When you pass a basic type value to a parameter, the passed value is copied to a local variable. When a value of the reference type is passed to the parameter, the address in the memory of the value is copied to a local variable. Therefore, the change of this local variable is reflected outside the function. It is clearly stated in the book that parameter transmission is passed by value, but I have to say that the reference type is not passed by value. please believe that I am not blind. When I read this part repeatedly, the ideological activity is like this: do not understand ---- slightly understand ---- Do not understand ---- think that the reference type is transmitted by reference ---- or do not understand ---- the header is about to blow up. It can be seen that the author seems to have made a part of the understanding that the transfer of the reference type is passed by reference, because the transfer of the reference type contains some shadow transmitted by reference, but the book clearly points out that it is passed by value. At this point, the problem arises: is the transfer of reference type values passed by value? Or, if it is passed by value, how should we understand it? If it is not passed by value, what should we do? (I don't think it is passed by value) I don't know how everyone understood this problem at the time, or how they understood it now. At that time, I was not very clear about the explanation in the book. There are a lot of answers to the JavaScript passing by value, but many of them come from Ctrl + C and Ctrl + V, some articles on the same issue explain different things, but then I found the Wikipedia and stackoverflow. Finally, I got my eyes. For more information, see Wikipedia and stackoverflow. You can preview the content in advance. For more information about Wikipedia, see English. Ii. analysis problem understanding about the transfer of reference type values (1). Transfer of basic type values first, briefly introduce the transfer of basic type values, you can skip the transmission of basic type values. First, the conclusion is given: the transfer of basic type values is passed by value. Here is a simple example:

function func(num){  num=10;  return num;}var outer=20;var result=func(outer);console.log(result,outer);

 

The printed results are 10 and 20 respectively. In this Code, the real parameter outer is passed to the function func as a parameter, so the real parameter is copied to the form parameter num. These two parameters are completely independent. In a function, the num value is assigned 10, but it does not affect the outer outside the function. outer is still 20 strong. This conclusion is well understood. The size of the basic type value is fixed and stored in the stack. We copy a basic type value or pass a basic type value to the form parameter of a function, it is equivalent to opening up a new memory space to store copies of the basic type value. These two values are completely equal and mutual operations do not interfere with each other. (2) Transfer of reference type values for the transfer of reference type, the author first gives the author an error at the time of understanding: the transfer of reference type values is passed by reference. Note: Here is an error understanding, just to better analyze the error. Eg1: Give an example first:
function setSex(obj){  obj.sex='girl';}var person = new Object();setSex(person);console.log(person.sex);

 

The printed result is: girl. The following error message is displayed: obj and person point to the same object, that is, they store the same reference. Modifying obj is equivalent to modifying person, so when the person parameter is passed to the function as a reference type parameter and the sex attribute of the object is modified, the sex attribute of the person is naturally girl. So you can see, isn't that passed by reference. Eg2: Another example is given:
function func(o2){  o2={name:'xiaoming'};  return o2;}var o1={color:'red'};var result = func(o1);console.log(o1,result);

 

This is also a transfer of reference type values. If the transfer of reference type values is passed by reference, both o1 and o2 point to the same object in the heap, the returned o2 should be the same as o1, but the printed result is like this: Object {color: "red"} Object {name: "xiaoming"} That is, o1 does not change with o2. Of course, the problem raised by the author at the beginning of the article is not solved here. Because the second example only raises an counterexample, and does not really explain, "Since you say this is not transmitted by reference, so what is the transfer ". Next, let's start with the question. Iii. Problem Solving note: the following content is referenced in Evaluation strategy of Wikipedia. If you are interested, click the link to view early adopters. This Evaluation strategy in Wikipedia introduces a variety of transmission strategies. Here I will mainly talk about three transmission methods related to this blog, call by value, call by reference, and call by sharing are passed by value, by reference, and by share. 1. call by value (call by value) It is relatively simple to pass by value. Here we will not repeat it here. A simple reference of this article is as follows: call-by-value evaluation is the most common evaluation strategy, used in versions as different as C and Scheme. in call-by-value, the argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function (frequently by copying the value into a new memory region ). if the function or procedure is able t O assign values to its parameters, only its local copy is assigned-that is, anything passed into a function call is unchanged in the caller's scope when the function returns. it is easy to understand the English section above. You can read it or pass it by yourself. 2. call by reference is also called pass-by-reference. It receives implicit references instead of parameter copies, the parameters and real parameters point to the same object. Therefore, when you modify the parameters, the corresponding real parameters are also modified. Some original texts are as follows: parameters es an implicit reference to a variable used as argument, rather than a copy of its value corresponds to the Eg2 mentioned above. The transfer of the reference type value of JavaScript is not passed by reference, and the change of the form parameter does not cause the actual parameter to change accordingly. Next, let's take a look at the last transmission method, that is, the JavaScript reference type value passing method that I agree to: call by sharing, passing parameters by sharing. 3. call by sharing. Note: Here is the focus !!! First reference: call by object-sharing "is an evaluation strategy first named by Barbara Liskov et al. for the language CLU in 1974. transfer by share, also known as call by object or call by object-sharing, is another information about CLU programming language proposed by Barbara Liskov of MIT in CLU in 1974, click here to go to Baidu encyclopedia. The parameter received by the function is a copy of the object reference, which is different from passing parameters by reference: Passing parameters by reference changes the form parameters, and the real parameters also change accordingly; pass by share. When you modify the attributes of a parameter, the real parameter changes accordingly. However, if you assign a value to the parameter, that is, allocating a reference to the parameter will not affect the external real parameter. Here are two examples: (a) The first example is the Eg2 mentioned above:
function func(o2){  o2={name:'xiaoming'};  return o2;}var o1={color:'red'};var result = func(o1);console.log(o1,result);

 

Print result: Object {color: "red"} Object {name: "xiaoming"} Here, the parameter of the reference type is passed by shared parameter, and transmitted by shared parameter according to the above mentioned, when modifying the attributes of a parameter, the real parameter changes accordingly. However, if you assign a value to the parameter, that is, allocating a reference to the parameter does not affect the external real parameter. Therefore, in this example, the external real parameter o1 does not change because of the re-assignment of the formal parameter o1. (B) The second example is similar to the first example, but a new one is added: function func (o2) {o2.color = 'blue'; o2 = new Object (); o2.color = 'white'; return o2.color;} var o1 = {color: 'red'}; var result = func (o1); console. log (o1.color, result); Modify the color attribute value of the parameter o2 to blue. According to the characteristics passed by reference, the color attribute value of the external o1 changes accordingly, and the next o2 = new Object () although it has the same name as the parameter o2, it does not point to the same object, that is, they are not the same reference (since they are new, they are naturally new, Hahahaha ), therefore, the external real parameters of the color Attribute Value of o2 will not change. After the function is called, The new o2 will be destroyed. Therefore, in this example, the printed results are blue and white. Next, I would like to add two more irrelevant things here to help us understand: first, this reference: It (call by sharing) is used by sort ages such as Python, Iota, java (for object references), Ruby, JavaScript, Scheme, OCaml, AppleScript, and other others. as mentioned in this article, pass by share is applied to many languages, including JavaScript, so that we also find the shadow about reference by share in JavaScript, it is a small validation of the above. Next is the reference of this section: However, the term "call by sharing" is not in common use; the terminology is inconsistent plain SS different sources. for example, in the Java community, they say that Java is pass-by-value, whereas in the Ruby community, they say that Ruby is pass-by-reference, even though the two extensions ages exhibit the same semantics. as mentioned in this article, shared references are not widely mentioned. In different languages, their names are not the same. For example, in Java, they are called Java passing by value. In Ruby, ruby Press In fact, they express the same meaning. Therefore, in JavaScript, the author prefers to regard the so-called "pass by value" about reference type transfer in JavaScript as a alias for transfer by share, however, it is different from passing basic type values in JavaScript by value. Summary: 1. the value-based transfer of reference type values in the book can be understood as passing a reference copy, and the object reference can be considered as a value (which is a value ), but I don't want to understand it like this. 2. The transfer method of reference type values in JavaScript is more inclined to pass by share. In this way, it will pass by value and by reference very well, but not separately, because the transfer of reference type values in JavaScript has some characteristics of transfer by reference in it, it is easy to confuse. 3. The author does not deny the statement of passing by value in the book, but is more inclined to pass by value of the reference type value as a alias for passing by share. If there are any errors in the content, please note that you are also welcome to talk about it in the comment area.

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.