Analysis of javascript function expression

  • 2020-12-13 18:51:22
  • OfStack

Start learning javascript function expressions and read on.

The function declaration is read before the code is executed, so the function declaration can be written below the function call:


 sayHi();
 function sayHi(){
     alert("Hi!");
}

2. Use the function expression to create the function, which must be assigned before calling:


 sayHi(); // Error!!!!! The function doesn't exist 
 var sayHi=function(){
     alert("Hi!");
}

3, the recursion

1 a recursion


function factorial(num){
        if (num <= 1){
          return 1;
        } else {
          return num * factorial(num-1);
        }
      }

arguments.callee refers to a pointer to a function being executed and can be used to achieve recursion:


function factorial(num){
        if (num <= 1){
          return 1;
        } else {
          return num * arguments.callee(num-1);
        }
      }

4. Closures (closures refer to functions that have access to variables in another scope).
A common way to create closures is to create another function inside one function. When a function executes, an execution environment and corresponding scope chain are created. A closure can only take the last value of any variable in the function:


function createFunctions(){
        var result = new Array();
        
        for (var i=0; i < 10; i++){
          result[i] = function(){
            return i;
          };
        }
        
        return result;
      }
      
      var funcs = createFunctions();
      
      //every function outputs 10
      for (var i=0; i < funcs.length; i++){
        document.write(funcs[i]() + "<br />");
      }

All the output in the above code is 10. This is because: every funcs function preserved createFunctions () the event object (this is a function that is also an object, also is a reference type Function type), and createFunctions () the active object has a variable i, so every funcs will have the variable i, while createFunctions () when the function returns the result, i has become 10. So each value of the funcs array is 10.

You can modify it like this:


function createFunctions(){
        var result = new Array();
        
        for (var i=0; i < 10; i++){
          result[i] = function(num){
            return function(){
              return num;
            };
          }(i);
        }
        
        return result;
      }

When each anonymous function is called, the current value of i is given to num, while inside the anonymous function, the closure of num is created and returned. Thus, each function that returns an array has one copy of its own num variable. (This paragraph is not clear, the reader will think again, if there is a better way to describe, please comment in the article below, thank you)

5. Object this

In a global function, this is equivalent to window.
When a function is called as a method, this corresponds to that object.
When each function is called, the function automatically gets two special variables: this and arguments. The inner function searches for these two variables only up to the active object.

6. Imitate block-level scope (private scope)
As follows:


function outputNumbers(count){
        for (var i=0; i < count; i++){
          alert(i);
        }
      
        alert(i);  //count
      }

      outputNumbers(5);

In languages like Java, the variable i in for is destroyed when it is used up. In javascript, the outputNumbers call generates an active object, and this i belongs to the active object, so it can be accessed anywhere within the function from the time it is defined, and it is common within the active object.

Syntax for anonymous functions (creating private scopes) :


(function(){
 // Here is the block-level scope 
})();

The function declaration is enclosed in parentheses to indicate that it is an expression, followed by parentheses to call it immediately.

If you need 1 variable temporarily, you can use a private scope:


function outputNumbers(count){
      
        (function () {
          for (var i=0; i < count; i++){
            alert(i);
          }
        })();
        
        alert(i);  //causes an error
      }

In the above code, i is kept private, and an error is reported when an anonymous function (private field) accesses i outside, even though alert is still inside the active object.

7. Private variables
The arguments, local variables, and other functions defined within the function are private variables of the function. Such as:


function add(num1,num2){
  var sum = num1 + num2;
  return sum; 
}

There are three private variables: num1, num2, and sum. Functions can access them internally, but not externally.

Privileged methods can access private variables: Simply put, give it a closure using an expression and access other functions inside the closure:


 sayHi(); // Error!!!!! The function doesn't exist 
 var sayHi=function(){
     alert("Hi!");
}
0

this. getName, ES107en. setName are expressions. After the instance of Person is created, the attributes of name and a can only be accessed through getName or setName.

Above is the whole content of this article, I hope to help you learn the study.


Related articles: