Recommendation 19: The comparison function calls and references to nature

  Before being called, JavaScript function is the only institution on the lexical meaning, there is no actual value at the time of pre-compiled function, it is only an internal analysis simply lexical, syntactic structure function, and a function identifier for a pre occupied as a function of space, its internal structure and logic has not been run. However, once the function is calling, its context will also born property. We can say that context is a function of a dynamic runtime environment, it is a dynamic concept, static concept of distinct functions. each function has a separate context (execution environment). look at the following example

function f(){
  var x = 5
  return x
}
var a = f
var b = f
console.log(a===b);  //true
function f(){
  var x = 5
  return function(){
    return x
  }
}
 var a = f
 var b = f
 console.log(a===b); //false

The following returns false, explanatory variables a and b is not identical to the variable, which is a function of the difference between a reference and a function call, the similarities and differences between them can explain FIG.

 

 

 

   The following describes the function reference essential difference between a function call. When the reference function is stored in a plurality of variables are the same entry point ARCA Thus, for the same function, no matter how many references to the function variables, these variables values ​​are the same, all for the entry point address of the function. for example, for the second example above, if the variables a and b are reference function F, instead of calling, if they are identical.

  Instead, this function is executed when a function call, and the return value to the variables a and b. That is, the variables a and b is the value stored, rather than a function pointer address to obtain entry.

  In the first example, the function call returns the value of the type of data (a value of 5), two values ​​are the same natural 5, but in the second example, the return value is a function call closure function, i.e. reference the type of data returned, although the closure structure is identical, but they are stored in pairs and different variables, i.e., they are completely different from the address pointer, the same can not look at the following example:

 function F(){
   this.x = 5
 }
 var a = new F()
 var b = new F()
 console.log(a===b);  //false
 function F(){
   this.x = function(){
     return 5
   }
 }
 var a = new F()
 var b = new F()
 console.log(a===b);  //false

  Relationship Function Example, as shown below can be copied by the new operator structures function to implement a function instantiated object. Examples of the process is actually a function structure during operation of assignment and initialization. Thus, when an instance of function F and assigned to a time variable, a function of the structure of a structure is not referenced in the original function, but in another area of ​​memory to run a function structure, nature is completely different.

Guess you like

Origin www.cnblogs.com/chorkiu/p/12123645.html