The passing of parameters in JavaScript

Source: Internet
Author: User

Variable Type

Before we get to the arguments, let's review the types of variables, which have 5 basic data types (Undefined, Null, Boolean, number, String) and reference data types (object,function, Array, etc.).

differences between the base type and the reference type

Declaring a variable of type string

var " string ";

Declare a variable of a reference type and add a property

var New  "Jeremy";

The difference is mainly in the way the content of the variable is saved, the basic type of variable is stored in a simple data segment, and the reference type variable stores a reference to the object, such as:

Basic type of variable copy, you can see that the basic type variable stored is the value of the variable

var 5 ; var num2 =//5

The NUM1 5 and num2 5 are completely independent, and these two variables can participate in any operation without affecting each other. Able to visualize the reproduction process.

Variable copy of a reference type, you can see that a reference to the object is stored in the reference variable

Object objects are stored in the heap, and obj1 and obj2 each store a reference to the object

var New  "Jeremy"; var obj2 ="James"//James

First, the variable obj1 holds a new instance of the object and adds a new property to Obj1 name, the Obj1 object is copied to Obj2,obj2 and has the Name property, and the address is the Name property of the Obj1. So when you modify the Obj2 attribute, the Obj1 Name property also changes. This relationship is shown between the variables stored in the variable object and the objects held in the heap.


parameter Passing

In JavaScript, whether it is a basic type or a reference type, the function arguments are passed by value, starting with the basic type:

function Test (num) {    ten;     return num;} var Ten ; var res =//10 External variables are not affected  //

Next look at the reference type:

function SetName (obj) {    "James";} var New  "Jeremy"; setName (person);

Console.log (person.name);//james affects external reference variables

At first glance, is this not just a reference pass? How can it be passed by value? It is passed by value because when you call SetName (person), you actually copy the reference to the object that the person points to, and then pass it to the SetName function. This will affect the object that this reference points to, that is, the object that the external person points to when the reference is manipulated inside the function SetName.

In general, the basic type of parameter passing replicates the specific value, while the reference type's parameter passing copies the reference to the object stored by this reference variable.

To further prove that parameter passing of a reference type is passed by value rather than by reference, see:

 function setName (obj) {obj.name  =  " james   "     = new   Object (); Obj.name  =  leon   " "  var  person = new   Object ();p erson.name  = "  Span style= "color: #800000;" >jeremy   ;setname (person); Console.log (person.name);  // james  

The above code output is James, if passed by reference, then the above code output is Leon. In fact, when obj.name = "James" is executed, the value of the object pointed to by the reference has changed, and when the obj is overwritten, the value of obj is a reference to the local object, which cannot affect the external object. And this object is destroyed after the function execution ends.

The passing of parameters in JavaScript

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.