JS parameter passing

Source: Internet
Author: User

Recently read the "JavaScript Advanced Program Design" encountered the problem of JS transfer mode, took some time, but finally understand.

Data type

The data types in JavaScript can be divided into two categories:

    • The base type value is primitive type, such as undefined,null,boolean,number,string.
    • A reference type value , that is, an object type, objects, such as Object,array,function,date.

Replication of variables

It is well known that the basic and reference types of variables in JS are saved differently, which makes the variables different when they are copied. If the value of the base type is copied from one variable to another, the value of the former is cloned one and the cloned value is assigned to the latter, so the two values are completely independent, except that their value is the same.

var num1 = 10;var num2 = Num1;console.log (num2);//10

The value stored in the above NUM1 is 10, and when the value of NUM1 is assigned to NUM2, the num2 value is also 10. But the two 10 are completely independent, the num2 10 is cloned, the equivalent I wrote a Word document, put it in the Num1 folder, and then I copy the Word document, called Word Copy, and then put this copy in Num2 folder, These two Word documents are exactly the same, and none of the modifications will affect 21.

Num2 + = 1;console.log (NUM1); 10console.log (NUM2); 11

As you can see from the above, the value of the modified num2 has not changed num1. Then look at the copy of the reference type. When a value of a reference type is copied from one variable to another, the value stored in the variable object is also copied into the space allocated for the new variable.

var obj1 = {  name: "111"};var obj2 = Obj1;console.log (obj2.name);//111
Obj2.name = "222";
Console.log (Obj1.name); 222

The first print out of the result is "111", this is easy to understand, but the second time to print out is "222", a little confusing. This is the difference between the reference type and the base type. Copying an object does not make an identical object in the heap memory, just a variable that holds pointers to the object. Copy the value of Obj1 to Obj2, and the copy of the value is actually a pointer to an object stored in the heap, which means that a new memory address is created to the OBJ2,OBJ1 and obj2 two variables pointing to the same object, and when the object is changed, Their values change, meaning that any change they make will be reflected in the other. The following simple diagram may be clearer.

Passing of function parameters

In the JS advanced programming, this is the argument passed: All function parameters are passed by value, that is, the values outside the function are copied to the parameters inside the function, and the value is copied from one variable to another variable. So if you can understand the replication of variables, then the transfer of parameters is very simple. Let's start with an example of a basic type.

var count = 10;function num (num1) {   num1 = 1;
return NUM1;} var result = num (count1); Console.log (result);//1
Console.log (count);//10, it doesn't turn 1.

This example is easy to understand, actually creates a copy of count, and then passes the value of count to the parameter because the value of the parameter is defined in the function, so 1 overwrites 10, and the last result returns 1, and count does not change. See an example of passing objects.

var person  = {    name: "Tom"};function obj (PEO) {    peo.name = "Jerry";
return PEO;} var result = obj (person);
Console.log (result.name);//Jerryconsole.log (person.name);//Jerry

In the example above, the person is copied into obj (), PEO and person point to the same object, and the Name property is modified in PEO, in fact, the Name property of the object they are pointing to is modified, and the Name property referenced by the corresponding external person changes. , so print it out for Jerry. In fact, at first glance, the feeling of the reference type parameter is passed by reference, which is the first mistake I made. Let's look at one more example.

var person = {    Name: "Tom"}; function obj (PEO) {    PEO = {       name: "Jerry"    };    return PEO;} var result = obj (person);
Console.log (result.name);//Jerry
Console.log (person.name);//Tom

In the above example, an object is redefined in the function, that is, there are now two objects in the heap memory, the external person points to the old object, the incoming parameter points to the newly defined object, so the value returned after the call is the value of the newly defined object. If the argument is passed by reference, then person.name prints the result of Jerry, from which the parameters are passed by value (some are referred to as shared delivery).

We took the Lao Luo recommended "A Brief History of mankind" to visualize it, not very good description. The first chapter of the history of the title is "Cognitive Revolution", we changed its name to "person" according to the number of pages can be directly found "cognitive revolution" content "is Peoson point to the object", the second chapter is "Agricultural Revolution", we put it as "result", its sub-catalogue has a section "Memory overload (renamed "PEO"), you can also find this section directly on the page number. Now we copy "person" into "PEO", the second chapter of "PEO" This section becomes "person", and we according to the first chapter "Peoson" to find the content of the first chapter, this is because they point to different content plate, do not interfere. In this case, heap memory is the content of each chapter, and the first and second chapters are 2 different objects, and the two are irrelevant, so when you print the external person.name, the result is still the property value of the previous object.

Conclusion

In summary, the parameters are passed by value in JS. I've made some rough examples, and the examples in JavaScript advanced programming are much clearer and easier to understand.

JS parameter passing

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.