JavaScript execution (D): Completion type
Infrastructure statement is any programming language, like JavaScript objects, JavaScript statement also has "looks a lot like other languages, but in fact nothing like" features.
Completion type
-
In the
try
middle there isreturn
the statement,finally
the contents will be implemented?function foo(){ try{ return 0; } catch(err) { } finally { console.log("a") } } console.log(foo());
finally
Indeed carried out, butreturn
the statement also came into force,foo()
returned a result0
.Although the
return
execution, but the function does not return immediately, and the implementation of thefinally
contents inside, such behavior contrary to many people's intuition. -
In
finally
addingreturn
the statement, what happens then?function foo(){ try{ return 0; } catch(err) { } finally { return 1; } } console.log(foo());
finally
Thereturn
"coverage" oftry
thereturn
. Perform a function twicereturn
, this is beyond many people's common sense, but also the other kind of behavior does not occur in the language.
Faced with such bizarre behavior, we can certainly use it as an isolated knowledge to memory, but in fact, behind a set of mechanisms in operation
This mechanism is the basis of JavaScript completion status of execution of the statement, we use a standard type to represent: CompletionRecord. (Completion Record for describing an anomaly, and the like out of the process execution statement)
Completion Record` shows the results after the completion of a statement is executed, it has three fields:
- [[type]]
indicating the completion of the type, there are break
, continue
, return
, throw
and normal
several types;
- [[value]]
The statement is the return value, if the statement is not, it is empty
;
- [[target]]
represents the target sentence, usually a JavaScript tag
JavaScript is relying on Completion Record type statement, just be in a complex nested structure of statements, the various controls.
Category statement
-
General statements
-
General statement is executed, executed sequentially from front to back (we'll ignore
var
the pretreatment mechanism and function declaration), without any branch or repeat execution logic. -
After the general statement is executed, it will get
[[type]]
tonormal
the Completion Record, JavaScript engine encountered such a Completion Record, we will continue with the next statement. -
General statement, only the expression statement will produce [[value]], of course, from the perspective of engine control, this value is not useful.
-
The use of chrome comes with debugging tools, you can know, enter an expression, you can get the results in the console, but preceded by
var
, becomesundefined
. Completion is the statement of the console displays the Record of Chrome[[value]]
.
-
-
Statement block
-
Canadian statement block is a set of bracketed statements, it is a composite structure of a statement can be nested.
-
Statement block itself is not complicated, we need to note that the Completion Record of statement inside the statement block
[[type]]
if notnormal
, it will interrupt the statement block subsequent statement execution. -
In a block, if each statement is a
normal
type, then it will be executed sequentially. But if we insert in the block in areturn
statement, resulting in a non-normal
recorded, then the wholeblock
will become a nonnormal
. This structure ensures that the non-normal
completion type can penetrate complex statements nested structures, generates control effect.
-
-
Controlled statement
-
Control statements with type
if
,switch
keywords, which have different types of Completion Record react. -
Control class statement divided into two parts, one is the impact on their interior, such as
if
,switch
,while/for
,try
. The other is caused by external influences such asbreak
, ,continue
,return
,throw
with these two types of statements, the order will be effective execution and implementation of logic control code, which is our main program of work. -
Generally speaking,
for/while
-break/continue
andtry - throw
so logical combination of comparison, is more familiar, but, in fact, we need to control the statement withbreak
,continue
,return
,throw
four types of control statements and combinations of two effects produced.
Because
finally
the content must ensure the implementation, ittry/catch
is finished, even if the results obtained are non-normal
completion of recording type, it must also be executedfinally
.
Whenfinally
performing non also beennormal
recorded, then makefinally
the record as a result of the structure of the whole try. -
-
Labeled Statements
-
In fact, any JavaScript statements can be tagged, you can add a colon before the statement:
firstStatement: var i = 1;
Most of the time, this thing is similar comments, is of no use. The only effect is the time: the completion of the recording types
target
cooperates multilayer cycle for bounce.
-