Is JS passed by value or by reference?

Source: Internet
Author: User
Tags call by reference

Recently an interesting question has been encountered: "is the value in JS passed by value or by reference?" ”

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, 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.

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

voidModify (intPint*q) {P= -;//pass by value-P is a copy of argument A and only P is modified*q = -;//Q is a reference to B, and Q and B are all modified}intMain () {intA =1; intb =1; Modify (A,&B);//A is passed by value, B is passed by reference,//A has not changed, B has changed    return(0);}

Here we can see:

    • When a = p is passed by value, modifying the value of the parameter p does not affect the actual argument a,p just a copy.
    • b = q is passed by reference, and the value of parameter B is also affected when the value of the parameter q is modified.
Probe into 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). First by Barbara Liskov. Presented in the Glu language of 1974. 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:1= +; var o == 1//  1, modified true//  1, not due to o = True Change

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.

So the values in JS are neither passed by value nor by reference. The values in JS are passed by share.

Reference documents
    • Wikipedia.org-evaluation strategy
    • Http://dmitrysoshnikov.com/-Evaluation strategy
Blog Address
    • http://bosn.me/js/js-call-by-sharing/

Is JS passed by value or by reference?

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.