JS function arguments and application of higher order functions

Time:2021-7-26

Function member – arguments

  • Arguments: the collection of all arguments (class array object) passed in when the function is called
  • Arguments. Callee: an attribute of arguments that points to the function itself
  • Function name. Caller: the caller of the function. If it is called globally, the returned caller is null.
  • Function name. Length: number of formal parameters
  • Function name. Name: the name of the function
//Arguments records all arguments
        //Use arguments to imitate and make a method to find the maximum number
        function max(){
            var maxNum = arguments[0];
            for(var i = 0;i < arguments.length; i++){
               if(maxNum < arguments[i]){
                   maxNum = arguments[i];
               }
            }
            return maxNum;
        }

Higher order function

  • If a function can be used as a parameter to another function, it is a higher-order function

    function fn(fn2){
                Console.log ("execute FN function first");
                fn2();
            }
            fn(function (){
                Console.log ("FN2 is a high-order function. When executing fn2() in FN function, jump here for execution")
            })
  • If a function can be used as the return value of another function, it is a higher-order function

    //Function can be used as the return value of another function
            function outer(n){
                //Want to execute the inner function -- return externally
                return function inner(m){
                    console.log(n + m);
                }
            }
            //Execute the inner function externally
            //outer(10);// In this way, call outer to assign a value of 10 to N, and m will be fixed and will not change
            var fun = outer(100);// Set outer's execution = variable
            //What fun stores is equivalent to a return value -- a function
            fun(22);//122
    
            //The function defined inside the function can be called outside

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]