The function is running

  1. Function of a first-class functions

    1. Function is an object

    2. Function is a function, the function can

      1. Stored in a variable (function expression)

      2. Return from a function

      3. To another function (callback) as a parameter
    3. Higher-order functions higher-order function

      1. Return or accept another function other as a function of function parameters are called higher order function

        1. function alertThenReturn() {
              alert('Message 1!');
          
              return function () {
                  alert('Message 2!');
              };
          }
          
          const innerFunction = alertThenReturn();
          
          alertThenReturn();    // 显示 'Message 2!'
          innerFunction();        //显示 'Message 2!'
          alertThenReturn()();    //显示 'Message 1!' 然后显示 'Message 2!'
  2. Callback callback

  3. Scope

    1. Lexical scoping lexical scope and implementation of environmental execution context

      1. Block scope and function scope called lexical scoping

      2. When a function is run, it creates a new scope of operation time. This scope represents the context of this function is available for a set of variables that the function uses. This is the run-time scopes, namely the execution environment.
    2. Execution environment includes:

      1. Function parameters

      2. Local variables declared within a function

      3. Functions declared within the role of parent variables

      4. Global Variables
    3. Function scope function scope

      1. Block-level scope block scope

        1. ES6 implemented with block-level scope const keywords and let

        2. var x = 10;      
          // 这里输出 x 为 10      
          {      
             let x = 2;      
          // 这里输出 x 为 2      
          }      
          // 这里输出 x 为 10
          1. const
            1. This statement creates a constant. The value of the constant can not be reassigned to change, and can not be re-statement
            2. The scope can be global or local declaration block
          2. let
            1. Declare a local variable block-level scope, and optionally initialized to a value.
            2. Why let
              1. Like mathematics description, let x be an arbitrary
          3. Staging the dead
            1. Variables declared by let until their defined initialization is performed when access before initializing the variable causes ReferenceError.

            2. This variable is in the dead zone from the top to the staging of the initialization process.

            3. As the code ReferenceError

            4. function do_something() {
                console.log(bar); // undefined
                console.log(foo); // ReferenceError
                var bar = 1;
                let foo = 2;
              }
        3. Characteristics declared by the variable var keyword does not have block-level scope in {} and still allow access to the outside

          1. var x = 10;    
            // 这里输出 x 为 10    
            {    
               var x = 2;    
            // 这里输出 x 为 2    
            }    
            // 这里输出 x 为 2
      2. Function scope

        1. Function can access all global variables and all their external variables
        2. var globalNumber = 5;
          
          function globalIncrementer() {
            const localNumber = 10;
          
            globalNumber += 1;
            return globalNumber;
          }
          
          console.log(globalIncrementer());    // 6
          
          console.log(globalIncrementer());    // 7
          
          console.log(globalIncrementer());    // 8
          
          console.log(localNumber);    // ReferenceError: localNumber is not defined 
          // 这里localNumber在log函数的外部,因为无法取到localNumber的值,const定义的块级作用域
      3. The scope chain scope chain

          • When accessing the variable, JS engine will traverse the scope chain (look for the ordering of variables is linear), first check the innermost layer, and then view the outer scope, and finally to the global scope when necessary.
        1. Window object
          • Any global variables are declared as window objects (global object) of the property is accessed, which represents the outermost scope chain.
      4. Variable variable shadowing shadow

        1. When another variable variable scopes created with the same name, the local scope variables will shadow the variable domain of external action

          • var money = '¥';
            
            function myMoney() {
              var money = '$';
              console.log(money);
            }
            
            myMoney();
            console.log(money);
          • Pointing to '$' variables are declared within a function, the same variables are positioned outside the shadow scope, i.e., point to '¥' global variables
          • If you do not declare variables inside a function, only one assignment, will result in scope shadowing
        2. The same name as any overlap between the different execution environment variables, will pass from the inside to the outside the scope scopeTraversal scopeChain to resolve.
  4. Closure

    1. Lexical scoping lexical scoping

      • 'Lexical' refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Variables declared position lexical scope by the source (to write) to determine the variable No available here.
    2. 闭包 closure

      1. Lexical Environment (another pit)

        1. A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upom the lexical nesting structure of ES code. Lexical environment is a standard type, it is an identifier of a particular variable and function code embedded on lexical ES associated sleeve structure.
          • An integral lexical environment referenced by environmental record and possibly empty external lexical environment.
        2. function makeFunc() {
              var name = 'count';
          
              function func2() {
                  console.log(name);
              }
              return func2;
          }
          
          var output = makeFunc();
          output();    // 'count'
        3. func2 has not been executed, it is returned func1. Generally, This code can not operate normally, because the name in the local variables func1 finished, the name can not be accessed again. But, Why it works?
          1. Because the function of JS in the closure. This process retains its function scope of access is called closure.
            1. And the closure is a combination of a function created by the lexical function from the environment.
            2. Here "lexical environment" refers to the code written in the JS file.
          2. output is the function func2 instance reference, and still have access to instance variables func2 lexical scope thereof, i.e. access name.
    3. Function to retain its scope

      1. Identifier means a symbol used to identify an entity, have different meanings in different application environments. In programming languages, the identifier is the name used by the user program, for to variables, constants, functions, and so named statement blocks, in order to establish a relationship between the name and use.

      2. When using the identifier, the scope chain will be checked, to retrieve the value of the identifier. For the scope chain function access code identifier is a very powerful tool.

Guess you like

Origin www.cnblogs.com/wydumn/p/11575497.html