In-depth learning of functions in JavaScript

Many people may encounter the confusion of functional parameters during learning JavaScript, in the spirit of in-depth spirit, I want to find some answers in the source code but Before doing this, first make a few concepts. Abandoning the value transfer, reference delivery, etc., returning to English:

Call by reference && call by value && call by sharing

is the reference to the c ++ of the C ++, value transfer. The third relatively confused, the official explanation is Receives the copy of the reference to object. I use popular words:

Object can be understood as a collection of Key, the data pointing to Key is a reference nature (here you don’t think of a pointer implementation or C ++ reference implementation), the function receives a variable COPY, the variable contains the reference to Object, is a value transfer.

So very obvious, when the function transfer, the object we received is actually the copy of the arguments, so the direct change of the type is not feasible; because the key of the Object itself is referenced, So modifying Key’s pointing is feasible.

Simply come to a few pieces of code to prove

Code 1: Function can modify data pointed to by Key

Let func = Obj => { = ‘dosk’}; let Obj = {name: ‘alxw’}; console.log (obj); // {name: ‘ALXW’ } func (obj) console.log (obj); // {name: ‘dosk’}
CODE 2: 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 === Results

Let DEF = {Name: ‘ALXW’}; let func = Obj => {Console.log (Obj = == DEF)}; func (def); // true
  So the third code may have questions, since the OBJ is the replication of DEF, why === Can the operation be true? Not to say === Operation for Object compares the address in memory, if it is replication, it should be false?  So we returned to Google V8 source code to see this matter. 
In-depth Google V8

Let’s take a look at the source code is equal to the operation code part:

BOOL Object :: strictequals (object * That) {if (this-> isnumber ()) {if (! That-> isnumber ()) Return false; return numbequals (this, there);} 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 FALSE; RETURN SIMD128VALUE :: Cast (this) -> Equals (Simd128Value :: Cast (That);} Return this == That;}

 It should be the last case. In theory, if DEF and OBJ are different objects, then it should return false, which is not overdown above? In fact, it is not a thing, that is, the inside Google V8 is instantiated an object, it is dynamically instantiated, and we know if dynamically instantiated in the compilation language can only be included in the heap, that is, it can only be pointed. Quote. This conclusion is that the implementation of CLASS such as Local, Handle, I think it is too much trouble, there is a simple proof method, that is, search the source code to get all calls  Object :: strictequals  is directly incoming There is no address operation. 
However, someone will ask, since it is a reference to the value of the value contains the reference to the object, theoretically can modify the object, why can the third code cannot be modified?

Very simple truth, because our so-called operations at the JavaScript language logic level, just in the case of the instance of Google V8, it is impossible to operate this point (of course, potential BUG is not count -.-)


I think hereYou can reinterpret the call by Sharing:

Indeed, the time is transmitted, but the content contains the Object’s pointer, and cannot modify this pointer, he is shared by multiple variables.

to see the source code

V8_DepRecate_Soon (“Uses Be Version “, Local

Call (Local

Recv, Int Argc, Local
argv [])); v8_warn_unused_result maybelocal Call (local
 context, local  RECV, INT ARGC, Local      The above is the upcoming interface, and the version code I have encountered contains a lot of this is about to be abandoned. Code, look well. The focus is the second interface, which is the unique call of the function. The  local   will eventually call the C ++ bit copy, so a simple proof is the value transfer. 

Don't forget, our defined variables are similar to Handle

this form, so they The object is shared, and the variable we say is not directly referred to as the object of Object !!!

Last final

In short understanding It may be very difficult to get up, but in JFeatures can be determined at the Avascript language level, which is important.

The above is the function call in the JavaScript introduced by Xiaobian, I hope that everyone can help, if you have any questions, please leave a message, the small package will reply to everyone. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment