"Re-learn front end" study notes (6)

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 trymiddle there is returnthe statement, finallythe contents will be implemented?

    function foo(){
     try{
     return 0;
     } catch(err) {
     } finally {
     console.log("a")
     }
    }
    console.log(foo());
    

    finallyIndeed carried out, but returnthe statement also came into force, foo()returned a result 0.

    Although the returnexecution, but the function does not return immediately, and the implementation of the finallycontents inside, such behavior contrary to many people's intuition.

  • In finallyadding returnthe statement, what happens then?

    function foo(){
     try{
     return 0;
     } catch(err) {
     } finally {
     return 1;
     }
    }
    console.log(foo());
    

    finallyThe return"coverage" of trythe return. Perform a function twice return, 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, throwand normalseveral 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

Here Insert Picture Description
Here Insert Picture Description

  • General statements

    1. General statement is executed, executed sequentially from front to back (we'll ignore varthe pretreatment mechanism and function declaration), without any branch or repeat execution logic.

    2. After the general statement is executed, it will get [[type]]to normalthe Completion Record, JavaScript engine encountered such a Completion Record, we will continue with the next statement.

    3. General statement, only the expression statement will produce [[value]], of course, from the perspective of engine control, this value is not useful.

    4. 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, becomes undefined. Completion is the statement of the console displays the Record of Chrome [[value]].

  • Statement block

    1. Canadian statement block is a set of bracketed statements, it is a composite structure of a statement can be nested.

    2. Statement block itself is not complicated, we need to note that the Completion Record of statement inside the statement block [[type]]if not normal, it will interrupt the statement block subsequent statement execution.

    3. In a block, if each statement is a normaltype, then it will be executed sequentially. But if we insert in the block in a returnstatement, resulting in a non- normalrecorded, then the whole blockwill become a non normal. This structure ensures that the non- normalcompletion type can penetrate complex statements nested structures, generates control effect.

  • Controlled statement

    1. Control statements with type if, switchkeywords, which have different types of Completion Record react.

    2. 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 as break, , continue, return, throwwith these two types of statements, the order will be effective execution and implementation of logic control code, which is our main program of work.

    3. Generally speaking, for/while- break/continueand try - throwso logical combination of comparison, is more familiar, but, in fact, we need to control the statement with break, continue, return, throwfour types of control statements and combinations of two effects produced.

    Here Insert Picture Description

    Because finallythe content must ensure the implementation, it try/catchis finished, even if the results obtained are non- normalcompletion of recording type, it must also be executed finally.
    When finallyperforming non also been normalrecorded, then make finallythe record as a result of the structure of the whole try.

  • Labeled Statements

    1. 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 targetcooperates multilayer cycle for bounce.

Published 33 original articles · won praise 73 · views 2772

Guess you like

Origin blog.csdn.net/weixin_46124214/article/details/104202031
Recommended