How to understand that the parameters of a function in Javasript are passed by value

Source: Internet
Author: User

This article is based on the fourth chapter of the Red Book JavaScript Advanced Program design, the 4.1.3 Pass parameter subsection P70, to further understand the parameters of the function in Javasript, when the passed parameter is the object's delivery mode.

(combined with the personal understanding of the information, there is an incorrect place, I hope you point out, thank you!) )

The reference information is as follows:


First, let's take a quick look at what is passed by value and passed by reference.

Pass by value : Copies the values from the outside of the function to the parameters inside the function, rather copying the values from one variable to the other, which is not affected by the two variables. (Transfer memory copy)


1 function Addten (num) {2         num+=10; 3         return num; 4     }5     var count=20; 6     var result=Addten (count); 7     alert (count); //  - 8     alert (result); //  -

Pass by reference : passes a memory pointer.

By reference, when a value is passed to a parameter, the address in memory is passed, so the operation on the value in the local scope is reflected in the global scope. Because they operate on the same address.

However, in ECMAScript, when you pass a value of a reference type to a parameter, if you say "pass by value" directly, the result of the following code is confusing:


1 function setName (obj) {2 "linshuling";  3     }4    var person=New  Object (); 5   setName (person); 6    alert (;//linshuling

But if this is the case because it is not "passed by value" but "passed by reference", we cannot explain the following:


1  functionsetName (obj) { "Linshuling";3           obj=New  Object (); 4  "Lin"; 5       }6       varperson=NewObject ();7  setName (person);8     alert (; // linshuling 

So, I found another explanation on the Internet:

Call-by-sharing a copy of the reference.

When I re-read this explanation in the book, I found that this is consistent with the explanation in the book: when you pass a value of a reference type to a parameter, the value is copied to a local variable in an in-memory address, so the change of the local variable is shown outside the function. (note here that the address is copied to a local variable instead of directly to the address.) )

That's what I understand:

Combine the above understanding Code1,code2: "Linshuling" in Code1, which is reflected in theglobal scope because it actually operates on the same address, such as the address of a after copy of Add, because add and ad are the same value, it also affects a. But in Code2 obj=new Object (); "Lin"; this is actually rewriting obj inside the function, which overrides the add, but it does not affect the ad, and the value of add and ad is completely independent (from here to understand that the function's parameter pass is passed by value, and it's good to understand!) ), and the redefined obj here is a local variable that will be destroyed immediately after the function has been executed.

How to understand that the parameters of a function in Javasript are passed by value

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: 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.