JS basis for the transfer of parameters (value transfer, object delivery)

Source: Internet
Author: User
Tags call by reference

First, the concept

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, when a function is called, and between arguments and parameters.

Pass VS by value. Pass by reference

Pass-by-value is the most commonly used evaluation strategy: a function's formal parameter is a copy of the argument that was passed when it was called. Modifying the value of a parameter does not affect the argument.

When passed by reference (call by reference), the function's formal parameter receives an implicit reference to the argument, not the copy. This means that if the value of a function parameter is modified, the argument is also modified. Both points to the same value.

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

Pass-by-value due to the need to clone replicas each time, performance is low for some complex types. Both of these methods have their own problems.

Maybe a lot of people do the back end, all think of "reference passing", but actually not, causing the problem.

Delivery by share call by sharing

In general, the answer to the community wiki on stack overflow is that for the object type passed to the function parameter, if the copy reference is changed directly, it will not affect the original object; Then it will change to the original object.

Here's a good example.

function Changestuff (state1, state2) {  = ' changed ';   = {item: "Changed"};} var obj1 = {item: "Unchanged"}; var obj2 = {item: "Unchanged"};changestuff (Obj1, obj2); Console.log (Obj1.item);   // Obj1.item becomes ' changed '   Console.log (Obj2.item);  // Obj2.item is still ' unchanged '

Second, the example analysis explores the transfer mode of JS value

The basic type of JS is passed by value.

var a = 1; function foo (x) {    = 2///  still 1, not affected by x = 2 Assignment

Then look at the object:

var obj = {x:1}; function foo (o) {    = 3//  3, has been modified!

Description O and obj are the same object, and O is not a copy of obj. So it is not passed by value. But does this mean that JS objects are passed by reference?

Let's look at the following example:

var obj = {x:1}; function foo (o) {    =+///  is still 1, and obj has not been modified to.

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

Delivery by share call by sharing

To be precise, the basic type in JS is passed by value, and the object type is passed by share (call by sharing, also called by object passing, by object sharing). This evaluation strategy is used in Python, Java, Ruby, JS and many other languages.

The focus of this strategy is that when you invoke a function argument, 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).

It differs from passing by reference in 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.

var obj = {x:1}; function foo (o) {    =+///  is still 1, and obj has not been modified to.

However, although references are replicas, the referenced objects are the same. They share the same object, so modifying the property value of the Parameter object also affects the property value of the argument.

var obj = {x:1}; function foo (o) {    = 3//  3, has been modified!

For an object type, because the object is mutable (mutable), modifying the object itself affects the reference and reference replicas that share the object. For basic types, because they are immutable (immutable), there is no difference between shared delivery and per-value delivery (call by values), so that the JS base type conforms to both by-value delivery and to shared-by-share delivery.

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

  According to the evaluation policy passed by share, A and B are two different references (b is the reference copy of a), but the same value is referenced. Since the basic type number 1 here is immutable, it is said here that there is no difference between passing by value and by sharing.

Immutable (immutable) properties of basic types

The base type is immutable (immutable), and only the object is mutable (mutable).

For example, the numeric value 100, Boolean true, false, modifying these values (for example, turning 1 to 3, turning true to 100) does not make any sense.

More easily misunderstood, is the JS string. Sometimes we try to "change" the contents of the string, but in JS, anything that looks like a "modify" operation on a string value is actually creating a new string value.

var str = "abc"; str[//  "a"str[0] = "D"//  is still "ABC"; Assignment is invalid. There is no way to modify the contents of a string

Objects are not the same, objects are mutable.

var obj = {x:0= +; var o == 1//  1, modified true// 1, does not change because o = True

Here the variable obj is defined, the value is object, and the value of the Obj.x property is set to 100. Then another variable o is defined, the value is still the object, and the values of the obj and O Two variables point to the same object (a reference that shares the same object). Therefore, modifying the object's contents has an effect on both obj and O. However, the object is not passed by reference, and the value of O is modified by o = True without affecting obj.

Https://developer.mozilla.org/zh-CN/docs/Glossary/Primitive Primer

The base type (base value, base data type) refers to non-object and no method data. In JavaScript, there are 6 basic data types: String,number,boolean,null,undefined,symbol (ECMAScript2015 new).

Summary: value passing will not be modified, object passing, itself will not be modified, but property values can be modified and can only be modified by property

Related articles

1190000005794070

75949933

JS basis for the transfer of parameters (value transfer, object delivery)

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.