Whether the values in JavaScript are passed by value or by reference discussion _javascript techniques

Source: Internet
Author: User
Tags call by reference object object

Recently encountered an interesting question: "JS in the value is passed by value, or by reference to pass it?" ”

Before we analyze this problem, we need to understand what is passed by value (call by value) and what is passed by reference (call by reference). In computer science, this part is called the Evaluation Strategy (evaluation strategy). It determines how values are passed between variables, the function call, and the arguments between parameters.

passing by value VS. Passing by reference

Passing by value is the most common evaluation policy: a function's formal parameter is a copy of the arguments that are passed when invoked. Modifying the value of a formal parameter does not affect the argument.

When passed by reference (call by reference), the parameters of the function receive an implicit reference to the argument, not a copy. This means that if the value of a function parameter is modified, the argument is also modified. At the same time they point to the same value.

Passing by reference makes tracing of function calls more difficult and sometimes causes subtle bugs.

Passing by value is less performance for some complex types, due to the need to clone replicas each time. There are problems in both ways of transmitting value.

Let's look at a c example to see the difference between a value and a reference:

Copy Code code as follows:

void Modify (int p, int * q)
{
p = 27; Passing –p by value is a copy of argument A, only P is modified
*q = 27; Q is a reference to B, Q and B are all modified
}
int main ()
{
int a = 1;
int b = 1;
Modify (A, &b); A is passed by value, B is passed by reference,
A unchanged, B has changed
return (0);
}

Here we can see:

When a => p is passed by value, modifying the value of the formal parameter p does not affect the actual parameter a,p just a copy.
b => Q is passed by reference, and modifying the value of the parameter Q also affects the value of argument B.
Probe into the transfer mode of JS value
The basic type of JS, is passed by value.

Copy Code code as follows:

var a = 1;
function foo (x) {
x = 2;
}
Foo (a);
Console.log (a); is still 1, unaffected by x = 2 Assignment

Look at the object again:
Copy Code code as follows:

var obj = {X:1};
function foo (o) {
o.x = 3;
}
Foo (obj);
Console.log (obj.x); 3, it's been modified!

Description O and obj are the same object, O is not a copy of obj. So it's not passed by value. But does this mean that the object of JS is passed by reference? Let's look at the following example:
Copy Code code as follows:

var obj = {X:1};
function foo (o) {
o = 100;
}
Foo (obj);
Console.log (obj.x); is still 1 and obj has not been modified to 100.

If you are passing by reference, modifying the value of the parameter o should affect the argument. But the value of modifying o here does not affect obj. Therefore, the objects in JS are not passed by reference. So what is the value of the object in JS how to pass it?

pass by share call by sharing

To be exact, the basic types in JS are passed by value, and object types are passed by share (call by sharing, also called by object, shared by object). First by Barbara Liskov. Presented in the Glu language of the 1974. This evaluation strategy is used in a variety of languages such as Python, Java, Ruby, JS, and so on.

The focus of this strategy is that when a function pass is invoked, the function accepts a copy of the object argument reference (neither a copy of the object passed by value nor an implicit reference passed by reference). The difference between it and by reference is that the assignment of a function parameter in a shared pass does not affect the value of the argument. As in the following example, you cannot modify the value of obj by modifying the value of the parameter O.

Copy Code code as follows:

var obj = {X:1};
function foo (o) {
o = 100;
}
Foo (obj);
Console.log (obj.x); is still 1 and obj has not been modified to 100.

However, although the reference is a copy, the referenced object is the same. They share the same objects, so modifying the property values of the parameter objects also affects the property values of the arguments.
Copy Code code as follows:

var obj = {X:1};
function foo (o) {
o.x = 3;
}
Foo (obj);
Console.log (obj.x); 3, it's been modified!

For object types, because objects are mutable (mutable), modifying the object itself affects the reference and reference copy of the shared object. For the basic types, because they are immutable (immutable), by sharing and by value delivery (call through value) there is no difference, so the JS basic type is not only consistent with the transfer by value, but also in accordance with the share delivery.
Copy Code code as follows:

var a = 1; 1 is number type, immutable var b = A; b = 6;

According to the evaluation strategy for shared delivery, A and B are two different references (b is a reference copy of a), but they refer to the same value. Since the basic type Number 1 is immutable here, it says there is no difference between passing by value and sharing by share.

immutable (immutable) properties of basic types

The base type is immutable (immutable), and only objects are mutable (mutable). For example, a numeric value of 100, a Boolean value of True, false, it does not make sense to modify these values (for example, turning 1 to 3, and turning true to 100). More easily misunderstood, is a string in JS. Sometimes we try to "change" the contents of a string, but in JS, any "modify" operation that appears to be a string value actually creates a new string value.

Copy Code code as follows:

var str = "ABC";
STR[0]; A
Str[0] = "D";
Str is still "ABC"; the assignment is invalid. There is no way to modify the contents of a string

objects are different, and objects are mutable.
Copy Code code as follows:

var obj = {X:1};
obj.x = 100;
var o = obj;
o.x = 1;
obj.x; 1, was modified
O = true;
obj.x; 1, does not change by O = True

This defines the variable obj, the value is object, and then sets the value of the Obj.x property to 100. Then define another variable o, the value is still the object object, at which point the value of obj and O Two variables points to the same object (a reference that shares the same object). So modifying the contents of the object has an effect on both obj and O. However, the object is not passed by reference, and the O value is modified with o = True and does not affect obj.

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.