JS function Pass Parameter

Source: Internet
Author: User

Perhaps everyone is not too concerned about the parameters of the function, simply, copy the values outside the function to the parameters inside the function, and copy the value from one variable to another. In-depth study, you will find that it is not so simple, this communication is to be divided into two kinds of situations (in fact, this is a wrong argument,ECMAScript all the functions of the parameters are passed by value -"Elevation 3", the reason is that two kinds, because it is easier to understand the combination of reference to the argument)-- The value and reference parameters are passed.

The value argument is for the base type, the reference parameter is for the reference type, and the argument can be interpreted as copying the variable value. After the basic type is copied, the two variables are completely independent, and neither side changes will affect the other; The reference type copies the reference (that is, the pointer), and any subsequent changes are mapped to the other party.

Many people are confused that parameters are passed by value, because access variables are both by value and by reference. Here's how it's different:

  

  This paragraph is important : we can think of the parameters of the ECMAScript function as local variables. When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, or, in the ECMAScript concept, an element in the arguments object). When you pass a reference type to a parameter, the value is copied to a local variable in the memory address (pointer), so the change of the local variable is reflected outside the function.

  1. Pass by value

1 function addten (num) {2     num + = 10;3     return num;4}5 6 var count = 20;7 var result = Addten (count);  Pass num = count8 alert (count) by value;  20, unchanged 9 alert (result);  30

Well understood, because it is passed by value, after passing the two variables are irrelevant!

 2, by reference to pass (so called easy to understand, in fact, is also passed by value)

1 function setName (obj) {2     obj.name = "Nicholas", 3}4 5 var person = new Object (), 6 setName (person)   ; Equivalent to passing  obj = person7 alert (person.name) by value;   "Nicholas"

  When var person = new Object () , you can use a relationship that represents a variable and an object:

When the function setName (person) is called, the global variables can be represented by the person and the local variable obj:

Create an object in the code above and save it in the variable person. The variable is then passed to the SetName (obj) function and then copied to obj. Inside this function, obj and person refer to the same object. In other words, even though ECMAScript says this variable is passed by value, obj accesses the same object by reference. Then, after adding the Name property to obj inside the function, the person outside the function will also react, because the person and obj at this point are pointing to the same heap memory address. Therefore, many people mistakenly think that the objects modified in the local scope are reflected in the global object, which means that the parameters are passed by reference.

  

To prove that the object is also passed by value, let's take a look at the following modified example:

1 function setName (obj) {2     obj.name = "Nicholas"; 3     obj = new Object ();//change the point of obj, at which point obj points to a new memory address, No longer points to the same 4     obj.name = "Greg"; 5}6 7 var person = new Object (), 8 setName (person);  You look at the bottom, believe me also by the value of the pass it 9 alert (person.name);  "Nicholas"

When you create an obj object , obj = new Object (); , take a look at the diagram of the person and obj at this point:

The only difference between this example and the previous one is that two lines of code are added to the SetName () function: obj = new Object (), which is used to change the direction of obj; obj.name = "Greg"; Used to add properties to the newly created obj. If passed by reference, the person is automatically modified to point to the memory address of the newly created obj, and the person's Name property value is modified to "Greg". However, when you access person.name, the result is "Nicholas". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj inside a function, the variable refers to a local object. This local object will be destroyed immediately after the function has been executed!

Although the variable person and the value of the parameter obj are the same object's address in memory, they are two independent variables. If the value of the parameter obj is changed in the function so that it points to another object in memory, the value of the variable person does not change or points to the original object.

Therefore, the passing of the reference type value parameter of a function in JavaScript is passed by value.

JS function Pass Parameter

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.