All function parameters in JavaScript are passed by value

Source: Internet
Author: User
Tags shallow copy

When you look at the advanced programming of JavaScript (third edition), pass the Parameters section, which mentions

The parameters of all functions in ECMAScript are passed by value

Its own explanation is that

Copy the values from the outside of the function to the parameters inside the function, just as you would copy the value from one variable to another.
The delivery of a primitive type value is like a copy of a primitive type variable.
The passing of a reference type value is the same as a copy of a reference-type variable.

We first understand a few concepts and then we discuss them later.

Data type

Basic data type, 6 types, Undefined, Null, Boolean, number, String, Symbol

Reference type, Object, Array, Date, Regexg, function, and so on

Data type description, you can refer to the JavaScript data type very 6+1

Memory space
var a = 2var b = 'abc'var c = truevar d = {value : 1}var e = [1, 2, 3]

Define the above variables, in memory, the space occupied by the schematic:

The underlying data type value that stores the actual value in the stack. Reference data type values, store reference address values in the stack, and store the actual values in the heap.

Assignment Copy/copy

Base data type, assignment copy, copy actual value

var a = 1var b = ab = 2console.log(a) // 1

Reference type, assignment copy, copy reference address, shallow copy. You can refer to the shallow copy shallowcopy and deep copy deepcopy in JavaScript

var a = {value : 1}var b = ab.value = 2console.log(a.value) // 2
Example analysis

Having understood the above concepts, let's say that copying the values from the outside of the function to the parameters inside the function is the same as copying the value from one variable to another. That is, an assignment copy process. Let's give a few examples to analyze.

1. Passing the underlying type, not modifying the parameter type in the function
var a = 1function func(o) {    o = 2    console.log(o)}func(a) // 2console.log(a) // 1

Here the Func function passes a parameter o, and the argument is actually a local variable of the function. So, we can modify the function

var a = 1function func() {    var o = a // 函数内部的参数变量,赋值函数外部的值    o = 2 // 修改内部变量的值    console.log(o)}func(a) // 2console.log(a) // 1

Can get the same result. Their changes in memory, motioned:

From the in, we can clearly see that the variable a has been equal 1 to, and the variable o , because of the value of the copied after the a actual value, in memory opened up space, stored in the stack. The function is then executed, and the func o value of the variable is modified, affecting itself only.

2. Passing the underlying type, modifying the type in the function
var a = 1function func(o) {    o = {value : 1}    console.log(o)}func(a) // {value: 1}console.log(a) // 1

In the same vein, we can modify the function here.

var a = 1function func() {    var  o = a // 函数内部的参数变量,赋值函数外部的值    o = {value : 1} // 修改内部变量的值    console.log(o)  // {value: 1}}func() // {value: 1}console.log(a) // 1

In-Memory changes:

From the in, we can clearly see that the variable a has been equal 1 to, and the variable o , due to the internal parameter assignment, copied a the actual value, in memory opened up space, stored in the stack. The function is then executed, and the func o value of the variable is modified, affecting itself only.

3. Passing a reference type, without modifying the type in the function
var a = { value : 1 }function func(o) {    o.value = 2    console.log(o)}func(a) // { value : 2}console.log(a) // {value : 2}

In the same vein, we can modify the function here.

var a = { value : 1 }function func() {    var  o = a // 函数内部的参数变量,赋值函数外部的值    o.value = 2 // 修改内部变量的值    console.log(o)  // {value: 2}}func() // {value: 2}console.log(a) // {value: 2}

In-Memory changes:

From the point of view, we can clearly see that because the variable a is a reference type, the value of the reference address is passed through the assignment of the internal parameters of the function, then the variable is pointed to the a o same memory object. Then execute the func function, modify the value of the variable o in the heap memory, and do not modify the value of the reference address in the stack. Thus, since variables o and variables a use the same reference address, that is, the value of the same heap memory, the a value of the variable will change as the variable o changes.

4. Passing reference types, modifying types in functions
var a = {value : 1 }function func(o) {    o = 2    console.log(o)}func(a) // 2console.log(a) // { value : 1}

Next, we can also modify the function here

var a = { value : 1 }function func() {    var  o = a // 函数内部的参数变量,赋值函数外部的值    o = 2 // 修改内部变量的值    console.log(o)  // 2}func() // 2console.log(a) // {value: 1}

In-Memory changes:

Since the variable a is a reference type, the value of the reference address is passed through the assignment of the function's internal parameters, and the variable is pointed to the a o same memory object. When the func function is executed, it changes the o data type of the variable, becomes the base data type, and then cuts off the reference. In this way, the variables are not related to each other a o .

Summarize

All function parameters in JavaScript are passed by value. The base type value, which is the actual value, the reference type, and the reference address value is passed.

Reference

JavaScript in-depth parameters are passed by value

Detailed Memory space

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.