A brief talk on the _javascript skills of JavaScript function parameter passing whether it is value transfer or reference transfer

Source: Internet
Author: User

In the traditional sense, the JavaScript function is thought to pass the reference pass (also known as the Pointer Pass), and some people think that the value transfer and the reference pass have. So JS parameter transmission in the end is how it? In fact, the following demo is also fully available for Java

First comes a relatively simple, basic type of delivery:

function Add (num) {
  num+=10;
  return num;
}
num=10;
Alert (Add (num));
AELRT (num);
Output 20,10

For the output 20 here, 10, according to JS official explanation is in the basic type parameter transmission, do a copy of the stack frame copy action, so that the external declaration of the variable num and function parameters of num, have exactly the same value, but have a completely different parameter address, neither know who, POPs the function argument num stack frame when the function call returns. So changing the function parameter num has no effect on the original external variables.

Let's look at a more complex delivery of object reference types:

function SetName (obj) {
  obj.name= "Ted";
}
var obj=new Object ();
SetName (obj);
alert (obj.name);
Output Ted

The essence of the above code is that it creates an object, assign its reference to obj (which is directly an assignment of a memory address in C), and then, when passing the function arguments, do the same thing as the previous method, copying a stack frame to obj for the function argument, both of which have the same value ( It can be interpreted as the address of an object, and then, when SetName makes a change, it actually changes the value of the object itself (called a mutable class in Java), and after the change is complete, it also pops up the stack frame of the function parameter obj.

So the corresponding output is the value of the changed object

So perhaps some friends may ask, this can also be understood as a reference pass (pointer pass) AH? No, strictly speaking, there is no pointer in a language similar to Java, and in Java, the process is called a parsing process from a symbolic reference to a direct reference. In C, the pointer is a type with a fixed length (2 bytes in most C compilers), but in a Java-like language, references also have their own properties and methods, but you cannot directly access and control it, so it is also an object in a sense, This mechanism also largely avoids memory leaks, and the term is called memory structured access mechanism.

To prove the above point, the above example is slightly modified:

function SetName (obj) {
  obj.name= "Ted";
  Obj=new Object ();
  Obj.name= "Marry";
}
var obj=new Object ();
SetName (obj);
alert (obj.name);
Output Ted

The only difference between this example and the previous example is that a new object is assigned to the function parameter obj, so that the function argument obj and the original reference obj parameter have completely different values and memory addresses.

The above is a simple discussion of the JavaScript function parameter transfer is the value of the transfer or the reference is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.