Many people may have encountered confusion about the method of passing function parameters when learning JavaScript. In the spirit of depth, I will share with you a tutorial about function calling in javascript, if you are interested in learning it together, many people may have encountered the confusion about the function parameter transfer method when learning JavaScript. In the spirit of depth, I will share with you a tutorial on function calling in javascript. If you are interested, let's study it together.
Definition
Many people may have encountered confusion about the method of passing function parameters when learning Javascript. In the spirit of depth, I want to find some answers in the source code, but before doing this, first, clarify several concepts. Discard inherent terms such as value transfer and reference transfer, and return to English:
Call by reference & call by value & call by sharing
It is the reference transfer and value transfer in C ++ that we understand respectively. The third type is confusing. the official explanation is that the copy of the reference to object. Let me explain in plain words:
An Object can be understood as a set of keys. The Object's data pointing to the key is of reference nature (here we will not go into the pointer implementation or C ++ reference implementation ), the function receives a copy of a variable, which contains an Object reference and is a value transfer.
Obviously, the object parameters we receive when passing parameters in the function are actually copying real parameters, so directly changing the direction of the parameter is not feasible; because all the keys of the Object are referenced, it is feasible to modify the point of the key.
Proof
Just a few pieces of code to prove
Code 1: The function can modify the data pointed to by the key.
let func = obj => { obj.name = 'Dosk' };let obj = {name : 'Alxw'};console.log(obj); //{ name: 'Alxw' }func(obj)console.log(obj); //{ name: 'Dosk' }
Code 2: The function cannot modify obj
let func = obj => { obj = {} };let obj = {name : 'Alxw'};console.log(obj); //{ name: 'Alxw' }func(obj)console.log(obj); //{ name: 'Alxw' }
Code 3: Internal obj and external === the results are equal
let def = {name : 'Alxw'};let func = obj => { console.log(obj === def) };func(def); //true
Therefore, the third code may have doubts. Since obj is a copy of def, why can the = operation still be true? Isn't it true that the = operation compares the address in the memory of the Object? If it is a copy, it should be false?
So let's go back to the Google V8 source code to see this.
Go deep into Google V8
Let's take a look at the strictly equivalent operation code in the source code:
bool Object::StrictEquals(Object* that) { if (this->IsNumber()) { if (!that->IsNumber()) return false; return NumberEquals(this, that); } else if (this->IsString()) { if (!that->IsString()) return false; return String::cast(this)->Equals(String::cast(that)); } else if (this->IsSimd128Value()) { if (!that->IsSimd128Value()) return false; return Simd128Value::cast(this)->Equals(Simd128Value::cast(that)); } return this == that;}
It seems to be the last case. Theoretically, if def and obj are different objects, false should be returned. Didn't this overwrite the above description? Actually, no. One thing is ignored, that is, when Google V8 instantiates an Object internally, it is actually a dynamic instantiation, however, we know that in compiled languages, dynamic instantiation can only be performed on the heap memory, that is, it can only be referenced by pointers. This conclusion proves that the implementation of classes such as Local and Handle is involved. I think it is too troublesome. There is a simple proof that the source code can be searched to obtain all the calls.Object::StrictEquals Is passed in directly without the address operation.
However, some people may ask, since the variable passed by the value contains the reference of the Object, the Object can be modified theoretically. Why cannot the third-stage code be modified?
It is very simple because the so-called operations at the logic level of the Javascript language are just calling the Google V8 instance method, and it is impossible to do this (of course, potential bugs are not counted -. -)
Redefinition
I think we can explain it again here:
Indeed, it is a value transfer, but the content contains the Object pointer and cannot be modified. It is shared by multiple variables.
Another simple proof
Come and see the source code
V8_DEPRECATE_SOON("Use maybe version", Local
Call(Local
recv, int argc, Local
argv[]));V8_WARN_UNUSED_RESULT MaybeLocal
Call(Local
context, Local
recv, int argc, Local
argv[]);
The above is the interface that is about to be deprecated. I happen to see that this version of code contains a lot of this type of code that is about to be deprecated. Just look at it. The second interface is the only interface called by the function. InsideLocal C ++'s bit replication will be called eventually, so it can be simply proved that the value is passed.
May be important
Don't forget, the variables we define are similar.Handle In this form, objects between them are shared. The variables in Javascript do not directly refer to the Object instance !!!Last
In short, it may be difficult to understand or even cause errors, but it is important to determine the features at the Javascript language level.
The above is the function call in Javascript introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. I would like to thank you for your support for the script home website!
The above is the details of the Code for function calling in JavaScript. For more information, see other related articles in the first PHP community!