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 in 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. Passing by reference by value is the most commonly used evaluation policy: 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, sometimes causing 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 passing by value and reference:   void Modify (int p, int * q) {    p = 27;//pass by value-P is a copy of argument A and only P is modified &nbs P   *Q = 27; Q is a reference to B, Q and B are modified}int main () {    int a = 1;    int b = 1;    Modify (A, &b);  //A Pass by value, B by reference,                     //A has not changed, B has changed     RET Urn (0);}   Here we can see:  a. P = When passed by value, modifying the value of the parameter p does not affect the argument a,p just a copy of a. b = q is passed by reference, and the value of parameter B is also affected when the value of the parameter q is modified. To explore the way JS value is passed the basic type of JS is passed by value.  var a = 1;function foo (x) {    x = 2;} Foo (a); Console.log (a); is still 1, not x = 2 The effect of the assignment is then to look at the object:  var obj = {x:1};function foo (o) {    o.x = 3;} Foo (obj); Console.log (obj.x); 3, has been modified! Indicates that 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) {    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 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?   by share pass call by sharing accurately, the basic type in JS is passed by value, and the object type is passed by share (called by sharing, also known as pass by object, shared by object). 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 calling function arguments, 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) {    o = 100;} Foo (obj); Console.log (obj.x); is still 1, and obj has not been modified to 100. 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) {    o.x = 3;} Foo (obj); Console.log (obj.x); 3, was modified! For object types, because the object is mutable (mutable), modifying the object itself affects the reference and reference replicas that share the object. And for the basic type, becauseThey are immutable (immutable), and there is no difference between shared delivery and by-value delivery (call to value), so the JS base type is consistent with both by-value delivery and by-share delivery.  var a = 1; 1 is the number type, and 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 reference the same value. 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) nature of the basic 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[0]; "a" str[0] = "D"; str; is still "ABC"; the assignment is not valid. There is no way to modify the contents of a string and the object is different, and the object is mutable.   var obj = {x:1};obj.x = 100;var o = obj;o.x = 1;obj.x;//1, modified o = true;obj.x;//1, not changed by O = True   set here The value of the semantic variable, obj, 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.

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.