English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

In-depth analysis of whether JavaScript is passed by value or by reference (recommended)

Passing by value (call by value) is the most commonly used evaluation strategy: the function's formal parameters are a copy of the actual parameters passed at the time of the call. Modifying the value of the formal parameters does not affect the actual parameters.

  When passing by reference (call by reference), the function's formal parameters receive an implicit reference to the actual parameters, rather than a copy. This means that if the value of the function's formal parameters is modified, the actual parameters will also be modified. Both point to the same value.

  Passing by reference makes it more difficult to trace function calls and can sometimes cause subtle BUGs.

  Passing by value is less efficient for some complex types due to the need to clone copies each time. Both value passing methods have their own problems.

  JS basic types, which are passed by value.

var a = 1;
function foo(x) {
x = 2;
}
foo(a);
console.log(a); // still is1, not affected by x = 2Assignment affects

  Let's take a look at the passing of objects again:

var obj = {x : 1};
function foo(o) {
o.x = 3;
}
foo(obj);
console.log(obj.x); // 3, has been modified! 
var obj = {x : 1};
function foo(o) {
o = 100;
}
foo(obj);
console.log(obj.x); // It is still1, obj was not modified to100.

  It can be seen that the passing of an object's value is not by reference passing. In fact, by shared passing call by sharing, to be accurate, basic types in JS are passed by value, and object types are passed by sharing (call by sharing, also called object passing, object shared passing).

  The focus of this strategy is: when passing parameters to a function, the function accepts a copy of the reference of the object actual parameter (neither a copy of the object passed by value nor an implicit reference passed by reference). It is different from passing by reference in that: when assigning to the formal parameters of the function in shared passing, it will not affect the value of the actual parameter.

  In summary, basic types are passed by value, while for objects, the address pointed to by the object is passed in, which can also be considered as a special type of pass by value. If you operate on the properties of the object within the function, it is actually operating on the properties of the object pointed to by the object. However, if you operate on the whole object (such as: o =) 10(0 or o = []), which actually newly defined an object, the reference address of the actual parameter is the reference address of the new object, which has nothing to do with the original reference, so it will not cause any change to the original object.

The above is the full knowledge introduced by the editor to everyone about whether JS is passed by value or by reference (recommended). I hope it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time. Thank you very much for everyone's support for the Yell Tutorial website!

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) for reporting violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like