JS function

JS function

statement

    function function name () {
       //Function body
    }

transfer :

    Function name
    Function name()

demo

<script>
    function sayHello(){
        console.log("hello js")
    }
    sayHello()
</script>

parameter

Formal parameters: The parameters set when the function is declared. Function: Occupy a place and can only be used inside the function. Actual parameters: The parameters passed in when the function is called. Function: When the function is called, the value of the actual parameter is assigned to the formal parameter, so that the formal parameter has a value. In the function body,,, you can use the formal parameter directly!

//Function declaration with parameters
function function name (parameter 1, formal parameter 2, formal parameter...) {
 //Function body
}

//Function call with parameters
Function name (actual parameter 1, actual parameter 2, actual parameter 3);

Calculate the sum of 2 numbers

<script>
    function sum(n1,n2){
        console.log(n1 + n2)
    }
    sum(10,3)
</script>

return value

Note on function return value:

  • The statement following return is not executed.
  • The function can have no return value. If the function does not return, the return result is undefined.
  • There can be multiple parameters of a function, but only one return value.
//Declare a function with a return value
function function name (parameter 1, formal parameter 2, formal parameter...) {
 //Function body
  return return value;
}

//You can receive this return value through a variable
var variable = function name (actual parameter 1, actual parameter 2, actual parameter 3);

Calculate 2 numbers and return the result

<script>
    function sum(n1,n2){
       return n1 + n2
    }
    var r = sum(10,13)
    console.log(r)
</script>

Anonymous function

Anonymous function: A function without a name assigns an anonymous function to a variable so that it can be called through the variable

    var function name = function(){
     //Function body
    }
<script>
   var sum = function (n1,n2){
       return n1 + n2
    }
    console.log(sum(3,13))
</script>

Self-execution (anonymous function executes once): the first bracket: the anonymous function is protected, the second parenthesis: call

(Anonymous function) ()
(function () {
      console.log("self-executing function")
  })();

Need to pay attention to the semicolon of anonymous functions

<script>
    var a = 10;
    var b = 20;
    b = a;
    (function () {
        console.log(b);
    })();

</script>

Scope

Variable function area 全局作用域: In the script tag, the area outside the function is the global scope, and the variables declared in the global function are called global variables. Global variables can be accessed anywhere. 函数作用域: The area in the function is called the function scope, the variables declared in the function scope are called local variables, and the local variables can only be accessed in the current function.

Global variables: Outside the function, the variables declared in the script tag are global variables, which can be accessed anywhere. Local variable: The variable declared in the function is the local variable, and the local variable can only be accessed in the current function body. Implicit global variables: Variables that are not vardefined are also global variables, called implicit global variables.

Pre-analysis

js execution code is divided into two processes:

  • Pre-analysis process (variable and function promotion)
  • Code execution line by line
console.log(num);
var num = 1
console.log(num)

Pre-analysis process

  1. Promoting the variable declared by var to the front of the current scope will not promote the assignment.
  2. Promote the function declaration to the front of the current scope.
  3. If the function has the same name, the latter will overwrite the former.
  4. If the var declaration has the same name as the function declaration, the function takes precedence.
Reference: https://cloud.tencent.com/developer/article/1444775 JS Function-Cloud + Community-Tencent Cloud