The delivery of JavaScript

Source: Internet
Author: User

JavaScript uses a variable object to track the lifetime of a variable. The base type value is stored directly within the variable object, whereas the reference type value is stored as a pointer in the variable object, which points to where the actual object is in memory.
Delivery of basic type values
When you pass a primitive type value to a parameter, the passed value is copied to a local variable (that is, a named parameter, or an element in a arguments object).
function AddOne (num) {
num++;
return num;
}
var count = 1;
var result = AddOne (count);
Console.log (count); 1
Console.log (result); 2
In the above example, the value of the variable count is passed to the function's parameter num for use in the function, while the value of the variable count and the parameter num is the same, but they are two independent variables, changing the value of the parameter num in the function does not affect the value of the variable count outside the function.
Therefore, the delivery of the basic type value parameter of a function in JavaScript is passed by value.
Delivery of reference type values
function SetName (obj) {
Obj.name = ' Nicholas ';
}
var person = new Object ();
SetName (person);
Console.log (Person.name); ' Nicholas '
In the above example, the value of the variable person is passed to the function's parameter, obj, and a Name property is added inside the function for the parameter obj, and the function to the parameter obj causes the variable person outside the function to have a Name property. From the results, the transfer of the reference type value parameter of the function in JavaScript appears to be passed by reference.
However, this is not the case. The value of a variable person is a reference type value, so its value can be seen in the variable object as an address (or pointer) of an actual object in memory. After passing a parameter, the value of the parameter obj is also the address of the object in memory, so the value of the action parameter obj in the function refers to the object that is referenced by the value of the action variable person.
function SetName (obj) {
Obj.name = ' Nicholas ';
obj = new Object ();
Obj.name = ' Greg ';
return obj;
}
var person = new Object ();
var result = SetName (person);
Console.log (Person.name); ' Nicholas '
Console.log (Result.name); ' Greg '
If the parameter pass is passed by reference, in the example above, the function changes the object referenced by the value of the parameter obj, then the object referenced by the value of the corresponding variable person will also change. Changing the way a function is written may be more helpful in understanding the value of a parameter's passing.
function SetName () {
var obj = arguments[0];
Obj.name = ' Nicholas ';
obj = new Object ();
Obj.name = ' Greg ';
return obj;
}
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.

The delivery of JavaScript

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.