JS in the function pass parameters, whether the value of the pass or reference delivery?

Source: Internet
Author: User


Remember the truth: JS function Pass parameters, whether it is a simple data type, or reference data type, is the value of the pass!!
Here is an example of the JS Red envelope book:
  
 
  1. function setName(obj) {
  2. obj.name = "Nicholas";
  3. obj = new Object();
  4. obj.name = "Greg";
  5. }
  6. var person = new Object();
  7. setName(person);
  8. alert(person.name); // "Nicholas"

Some people will ask, since it is the value of the transfer, then I setname this function passed the person object, in the function to modify the person object, why the global person has an impact?
First, we need to be aware that this variable is the address of an object in the heap memory. So, the value passed to the function parameter ispoint to the address of the object in the heap memory,This is why the changes to this parameter inside the function are external, because they all point to the same object.
Take a look at the picture:
If it is a reference pass, the contents of the second box are passed through, there will be no fourth lattice, that is, if it is a reference pass, the person function parameter is a reference to the man variable, then the heap in memory that object has only one reference address;
The fact is that the value is passed, the parameter of the person function copies a copy of the address of the object in the heap memory, the object in the heap memory has two reference addresses, and if I modify the parameter inside the function, the object in the heap memory is actually modified;
Look at this question again:
  
 
  1. var b = {b:1};
  2. function addB(b){
  3. b.b++; // function parameter b is a pointer to the The object in the heap memory has been modified.
  4. b={}; // The function parameter B is pointed to another empty object, that object in heap memory is referenced only by the global B
  5. b.b=3; // Null object referenced by function parameter B is modified, and is destroyed when the line of code finishes executing
  6. }
  7. addB(b); // b变量对堆内存中对象的引用地址,被复制了一份,赋值到函数形参上
  8. console.log(b.b);




Null

JS in the function pass parameters, whether the value of the pass or reference delivery?

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.