Introduction to JavaScript – function (I)

Time:2021-12-2

JS function

Function in English is [function] and [number] function; duty; The meaning of a grand gathering
In JS, it means function. It’s called a method in Java.

Define function

Function fun (parameter){
        //Function body
        //Return optional. Add or not according to actual requirements
    }

Use function

//Function name ()
    fun();

ps:In JS, our method canCall before declareThat is, the code calling the cold element can be written in front of the definition function.

Argument, formal parameter

Argument: it is simply understood that the actual parameter is passed when calling the function
Formal parameter: it is simply understood that when defining a function, it is used to receive the value passed during the call

var n1=123 , n2=456
    //Call function
    fun(n1 , n2)

    //Define function
    function fun( a , b){
        a += 100
        b += 100
        console.log(a , b)
    }
    console.log(n1 , n2)
    
    //Operation results: 223, 556
    //123 , 456

PS: we can see that the argument and formal parameter are not the same variable

Less rigorous parameter list

Let’s start with a code:

function fun(a , b , c){
        console.log(a*10 , b+10 , c-10)
    }
    //Call
    Fun (1,2,3) // output result 10, 12, - 7
    //There's nothing wrong here. Normal output

    //But what about the following call???
    fun(20,2)
    fun()
    fun(12,32,14,5)

PS: there is no mistake in the above writing… If you don’t believe it, you can try it yourself.

  1. When we define a function, we define how many parameters. In principle, we should pass parameters strictly according to the specification, but there is no way. JS is more “tolerant”. You can receive as many parameters as you pass
  2. If you pass more parameters, ignore the parameters you pass more. The parameters before the first ‘,’ are calculated as the first parameter
  3. If the parameters are missing or not passed, they are undefined. It’s worth it if it’s not passedundefined

Return keyword

First of all, in JS, it is not stipulated that a function must have a return value or return value type when it is defined. It is very active. You can return any result you want
Secondly, if you want to return the result or end the function body, you need to use the return keyword

  1. Return is used to return the value of the function to the caller
  2. Return once executed, the current function ends immediately. Note:Must return a specific value to end(think about recursive calls)
  3. There can be multiple in the return function, which returns values under different conditions. For example, in switch, you can use return after each case to return a specific value

arguments

In Es5, arguments is an object used to receive all parameters. Its usage is consistent with that of an array; But for nowAbandoned, there are better alternatives
ES6: (recommended)

function fun( n1, n2, ...args ){
        //Args can only receive numbers after N2
        //Received with... Is an array that can be traversed
    }

PS:… You don’t have to write args after it. You can name it yourself and meet the string specification. It is generally represented by args

About the usage of… Args

  1. Args cannot be placed before the parameter list
  2. Args has one and only one

On the coupling of functions

Look at the code first:

var num = 100
    var res = fun(1)
    //Define function
    function fun(n){
        console.log( num + n )
    }
    //The output is 101

ps:The above code runs well, but,Coupling degreeVery high, because it is taboo to operate with global variables in the function body. If the project needs to be transplanted, these variables will be sloppy and have a high degree of coupling. This is easy to be done in future workStir fried squid
be careful: if you want to use, just pass the parameters. Never use global variables

Function name problem

var f = function fun(){
        console.log("my name is rainbow")
    }  
    console.log(f)

ps: if we assign a function body to a variable, what will happen if the variable is printed out?
Result: no error is reported, but the content of the whole function body is printed directly. We can think that when we call a function, we writefun(), that isFunction name ()It becomes a call. In fact, this is true in JS. Adding parentheses to the function name is a function call.
Here, you and I want to ask a question. This method body is assigned to a variable. What type is this variable???

Anonymous function

The simple understanding is that you want to use a function, but this function has no name. How do you use it.
It’s very simple. Didn’t it say?

  • The calling function is the function name ()
  • And becauseFunction name = method body
  • thatMethod body () = function name ()
    This calls the function once
var a = 10;
    var b = 20;****************
    var f = function(){
        console.log(n1+n2)
    };
    F (a, b) // output result 30

Self executing function

  • Self executing functions areDefine yourself and execute yourself
    This is the application of anonymous functions
(function(){
        console.log('this is rainbow !')
    })();
    example
    var a = 10;
    var b = 20;
    (function( n1 , n2 ) {
        console.log( n2 + n1)
    })(a ,b);
    //The output is 30

ps:

  • The body of a self executing function must be wrapped in (). It is suggested to add a semicolon ‘;’ at the end
  • Self executing application scenarios are generallyClass libraryupper