Today encountered a topic is about JS transmission, do the general, the other half wrong, this is the choice of the problem when it is wrong, no points! So if the foundation is not solid, then it is easy to make mistakes, and do the question when the heart is not the bottom, ambiguous,
So it is necessary to continue to review the basic knowledge in real time, so as to grow faster!
What is passed by value? What is passed 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.
Pass-by-value due to the need to clone replicas each time, performance is low for some complex types; passing by reference makes tracing a function call more difficult, sometimes causing subtle bugs.
Probe into the transfer mode of JS value
The basic type of JS, is passed by value, the following code can prove:
1 var a = 1; 2 function foo (x) {3 x = 2; 4 }5foo (a); 6 // still 1, not affected by x = 2 Assignment
For reference types, see the following code:
1 var obj = {x:1}; 2 function foo (o) {3 o.x = 3; 4 }5foo (obj); 6 // 3, has been modified!
Note that O and obj point to the same object, so it is not passed by value, but this can prove that the object in JS is passed by reference?? Teenagers can look at the following example:
1 var foo = {name: ' foo '}; 2 function Test (o) {3 o = {name: ' Bar '}; 4 }5Test (foo); 6 Console.log (foo.name);
The value of printing foo.name is still ' foo ', which means that the object in JS is not passed by reference. So messy ... What exactly is the object in JS to pass on?
Delivery by share call by sharing
Strictly speaking, 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. In essence, we can understand this:
When a function is called to pass a parameter of a reference type, the pass is a copy of the object reference, but the copy of the object reference refers to the same place as the original object reference (that is, the address that the object stores in memory), and you must understand this sentence carefully!!!!
As you can see, the following example confirms the idea of shared delivery.
1 var foo = {name: ' foo ' }; 2 function Test (o) { 3 o.name= ' test ' ; 4 o={name: ' Bar ' }
From the above results can be obtained first: the object is not passed by value, if it is passed by value, the value of the Name property of the printed Foo is not test, because passing by value is a copy of the object, the modification of the copy does not affect the meta-object, so you can prove that the object is not passed by value!
can also prove that the second:JS object is not completely by reference , if passed by reference when executing o={name: ' bar ' this line of code, the result of Foo printing should be obeject {name: ' Bar '}, And the result is Object {name: ' Test '}, so the two-point JS citation
A type is passed by share when passed as a parameter, that is, the pass is a copy of the original object reference, but the copy is pointing to an in-memory object with reference to the original object!
Before learning that the underlying type in JS is passed by value, the reference type is passed by reference, and not fully understand the way the parameters are passed in JS, so the understanding of the basic knowledge of JS is very important, must grasp firmly, understand thoroughly, otherwise only will let oneself is a half bucket of water,
And not become a good front-end engineer!
Is the JS parameter passed by value or by reference?