JavaScript execution (C): function
In JavaScript, the most important scene is the context switching function call.
function
In the ES2018, the function is already a very complex system of.
-
Ordinary functions: a function defined with the function keyword
function foo () { // code }
-
Arrow functions: with => operator-defined function
foo = () => { // code }
-
Method: functions defined in the class
class C { foo(){ //code } }
-
Generator function: a function definition function *
-
Class: The
class
class definition, and indeed functionclass Foo { constructor(){ //code } }
-
Six / seven / eight kinds asynchronous function: an ordinary function, arrow and function generator function plus
async
keyword.async function foo(){ // code } const foo = async () => { // code } async function foo*(){ // code }
Ordinary variables, these functions and there is no essential difference, is to follow the rules "is defined to inherit environment", one of their behavioral differences that this
keyword.
Behavior this keyword
this
Is a keyword in JavaScript, its use is similar to a variable. But this
now there are many different behavioral variables.
this is a very important part of the implementation of context. With a different function call, this value are different because
Normal function this
is determined by the "reference used to call it" the mystery lies: we get the expression function, which actually returns is not the function itself, but a Reference
type. Reference
Type consists of two parts: an object and an attribute value.
Reference used when calling the function, this value determines the execution time of the function.
From the perspective of the runtime view, this
expression uses no connection with the object-oriented, which is associated with a function call when.
Generator function, asynchronous induction generator function and a normal function like an ordinary function behavior is consistent asynchronous function arrow arrow function and behavior are consistent
Mechanisms for this keyword
Function can reference variables definition above analysis, the function also can remember when definitions this
, therefore, there must be a function of the internal mechanism to hold this information.
In JavaScript standard, predetermined as a function of time to save the context of the definition of private property [[Environment]]
.
When a function is executed, creates an execution environment of the new recording, the recording layer of the lexical environment (outer lexical environment) is arranged to function [[Environment]]
. This action is context switching
JavaScript with a stack to manage the execution context, this stack each also contains a list. As shown below:
When the function is called, will push a new execution context, at the end of the function call, execution context is popped.
And this
it is a more complex mechanism, JavaScript standard defines [[thisMode]]
private property.
[[thisMode]] private property has three values :
lexical
: Get this represents from the context, this corresponds to the function arrow.
-global
: indicates when this is undefined, taking global object, corresponding to the ordinary function.
-strict
: When strict mode use, this time in strict accordance with the value of the incoming call, which may be null or undefined.
Interestingly enough, behavioral approach with the regular functions are different, it is precisely because the class
design has become by default strict
execution mode
When the function to create the lexical environment record a new execution context, it will be based [[thisMode]]
to mark a new record of [[ThisBindingStatus]]
private property.
Code execution encountered this
when, layer by layer would check the current lexical environment record [[ThisBindingStatus]],
has to find when this
acquired when environmental record this
values.
The operation of this built-in functions
Function.prototype.call
And Function.prototype.apply
can be passed in a function call is specified this
value
function foo(a, b, c){
console.log(this);
console.log(a, b, c);
}
foo.call({}, 1, 2, 3);// {} 1 2 3
foo.apply({}, [1, 2, 3]);// {} 1 2 3
Here call
and apply
effect is the same, but are different ways of mass participation