Is the JS parameter passed by value or by reference?

Source: Internet
Author: User
Tags call by reference

Data type base data type

The values of number, String, Boolean, null, undefined base data type are stored in the stack;

Reference data type

Objects such as array, object, function, and data for reference types are divided into two parts:

    • Address
    • Value

Where the stack is a reference to the save address, the heap holds true values such as

What is passed by value? What is passed by reference?

Pass by value: The function's formal parameter is a copy of the argument that is being invoked, and modifying the parameter's value does not affect the argument.

When passed by reference (call by reference), the function's formal parameter receives an implicit reference to the argument, not the copy. This means that if the value of a function parameter is modified, the argument is also modified, and both points to the same memory address.

JS delivery mode

Parameters are basic data types:

1     var a = ten; 2     function Add (a) {3         a = +; 4         Console.log (a); //  - 5     }6    Add (a); 7     Console.log (a); // Ten

Variable a changes the value inside the add, and does not affect the external a variable;

Parameters are reference types:

1 //Reference type2     varperson = {name: ' Joel ', age:11}3     functionfoo (o) {4O.name = ' alen ';//change the value of an object5o = {name: ' Elire '}//change the address of an object6Console.log (O.name);//Elire7     }8 foo (person);9Console.log (Person.name);//Alen

If it is passed by reference, the O object passed in will be the same memory as the external person object, but the internal o.name of foo () is Elire, and the external person.name is alen, so it is not passed by reference even when the reference type data is passed;

Keep looking at the demo below:

1     varV1 = []2     varV2 = {};3     varV3 = {};4     functiondemo1 (v1, v2, v3)5     {6V1 = [1];7v2 = [2];8V3 = {A:3}9     }Ten demo1 (v1, V2, v3); OneConsole.log (v1);//Blank AConsole.log (v2);//[Object Object] -Console.log (V3.A);//undefined

thus: v1, V2, V3 have not been changed, V1 is still an array of 0 elements, V2, V3 is still a blank object. That is, the reference type data is also passed by value, first copying the address in the stack, and then changing the direction of the address within the DEMO1 ().

However, it is different whether the reference type is passed by value versus the base type data by value.

Basic data types, such as numbers, strings are copied directly into the value, and reference types such as arrays, objects are copied into the variable address.

Before we let v1, V2, v3 as parameters into the function, there is a copy of the address, these address copies of the point and the outside of the V1, V2, v3 address point is the same. But we've assigned values for V1, V2, v3, which means we've changed the point of the address copy to a new array and object. Such internal v1, V2, v3 and external v1, V2, v3 are completely broken.
If we do not assign a new value, but rather manipulate it directly, then the same array or object that is directed to the outer V1, V2, and V3 is still passed in, because we have not changed their direction.

Such as:

1     varV1 = []2     varV2 = {};3     varV3 = {a:0};4     functionDemo2 (v1, v2, v3)5     {6V1.push (1);7v2.a = 2;8v3.a = 3;9     }Ten  One Demo2 (v1, V2, v3); AConsole.log (v1);//1 -Console.log (v2.a);//2 -Console.log (V3.A);//3
Summarize

Basic data types, such as numbers, strings are copied into the values, and reference types such as arrays, the object is to copy the variable address in,

If there is no change in the address pointed to, then they are all pointing to the same memory address;

If you change the address point then they and the external object is two independent objects, do not interfere with each other;

Is the JS parameter passed by value or by reference?

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.