Does JS pass by value or by reference?

Source: Internet
Author: User
Tags call by reference

Does JS pass by value or by reference?
Recently I encountered an interesting question: "Is the value in JS passed by value or by reference ?" Before analyzing this problem, we need to know what is call by value and call by reference ). In computer science, this part is called Evaluation Strategy ). It determines how values are transmitted between variables and between real parameters during function calls. Pass by value VS. Pass by reference call by value is the most common evaluation policy: the function parameters are copies of the passed real parameters when called. Modifying the parameter value does not affect the real parameter. When a call by reference is passed, the function parameters receive implicit references of real parameters instead of copies. This means that if the value of the function parameter is modified, the real parameter is also modified. Both point to the same value. Passing by reference will make it more difficult to trace function calls and sometimes cause some subtle bugs. Because duplicate cloning is required each time for value-based transmission, the performance is low for some complex types. Both methods have their own problems. Let's first look at a C example to understand the difference between passing by value and reference: void Modify (int p, int * q) {p = 27; // pass by value-p is a copy of real parameter a, and only p is modified * q = 27; // q is a reference of B, both q and B are modified} int main () {int a = 1; int B = 1; Modify (a, & B); // a is passed by value, B is passed by reference, // a is not changed, and B changes return (0);} here we can see that when a => p is passed by value, modifying the value of parameter p does not affect real parameter a, and p is only a copy of parameter. B => q is passed by reference. Modifying the value of the parameter q also affects the value of the real parameter B. Explore the transmission mode of JS values. The basic type of JS is passed by value. Var a = 1; function foo (x) {x = 2;} foo (a); console. log (a); // still 1, not affected by the value of x = 2. Then, let's look at the object: var obj = {x: 1}; function foo (o) {o. x = 3;} foo (obj); console. log (obj. x); // 3, modified! O and obj are the same object, and o is not a copy of obj. Therefore, it is not passed by value. But does this indicate 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); // It is still 1, and obj is not modified to 100. if it is passed by reference, modifying the o value of the form parameter should affect the real parameter. However, the o value modified here does not affect obj. Therefore, objects in JS are not passed by reference. So how exactly does the object Value Pass in JS? Transfer by share call by sharing accurately indicates that the basic types in JS are passed by value, and the object types are passed by share (call by sharing, also called by object and by object sharing ). It was first proposed by Barbara Liskov In the GLU language in 1974. This evaluation policy is used in Python, Java, Ruby, JS, and other languages. This policy focuses on: When a function is called to transmit parameters, the function accepts copies referenced by the object's real parameters (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 function parameters in the shared transfer does not affect the value of the real parameter. In the following example, you cannot modify the obj value by modifying the o value. Var obj = {x: 1}; function foo (o) {o = 100;} foo (obj); console. log (obj. x); // It is still 1, and obj is not modified to 100. however, although the reference is a copy, the referenced objects are the same. They share the same object, so modifying the attribute value of the object will also affect the attribute value of the real parameter. Var obj = {x: 1}; function foo (o) {o. x = 3;} foo (obj); console. log (obj. x); // 3, modified! Because the object type is mutable, modifying the object itself will affect the reference and reference copies of the shared object. For basic types, since they are all immutable, transfer by share is no different from transfer by value, therefore, the basic JS types are both pass by value and pass by share. Var a = 1; // 1 is of the number type, and it cannot be changed. var B = a; B = 6; according to the shared transfer evaluation policy, a and B are two different references (B is the reference copy of a), but reference the same value. Because the basic type Number 1 here is immutable, there is no difference between passing by value and passing by share. Immutable (immutable) properties of the basic type is immutable, only the object is mutable ). for example, if the numeric value is 100, the Boolean value is true or false, modifying these values (for example, changing 1 to 3 and true to 100) makes no sense. It is easy to misunderstand that it is a string in JS. Sometimes we try to "change" the content of the string, but in JS, any seemingly "modify" Operation on the string value actually creates a new string value. Var str = "abc"; str [0]; // "a" str [0] = "d"; str; // still "abc "; the value assignment is invalid. There is no way to modify the content of the string, and the object is different. The object is variable. Var obj = {x: 1}; obj. x = 100; var o = obj; o. x = 1; obj. x; // 1, Modified o = true; obj. x; // 1. It does not change the definition variable obj because of o = true. The value is object and obj is set. the value of the x attribute is 100. Then define another variable o, and the value is still the object. In this case, the values of the obj and o variables point to the same object (share the reference of the same object ). Therefore, modifying the object content affects both obj and o. However, the object is not passed by reference. The o value is modified using o = true without affecting obj.

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.