current position:Home>JavaScript functions -- pre parsing, Iife, advanced functions, recursion

JavaScript functions -- pre parsing, Iife, advanced functions, recursion

2022-04-29 09:28:26Delete your code in the middle of the night·

Pre parse

(1) Variable pre parsing is also called variable promotion

(2) Function pre parsing is also called function promotion

When html File loading to script When labeling Will take the whole js Pre parse the file 、 belt var The variable will increase

Function enhancement

Ascending function Defined function

When the function is called Also do pre parsing

(1) Variable and function have the same name

The function is below the variable ---- Function ignored

The priority of function pre parsing is higher than that of variable pre parsing

No matter what function Wherever it is written, it will be on the variable

var a = 1;

            function a(){

                console.log(' also 2 God , You can play 5 God , Come again 9 God ');

            }

            console.log(a);

(2) Duplicate function names will overwrite

(3) Pre parsing of different function definition methods

Declare and define functions   The whole should be improved

Variable name and function name are the same --- Print the statement at the end

Function promotion takes precedence over variable promotion

var a = 1;

function a(){}

console.log(a);

analysis :

function a(){}

var a;

a = 1;

console.log(a);  ==》1

Variable name and function name are the same ---

When the function is promoted When the following variables are declared but not initialized Will be ignored

Print statements at the top

console.log(a);

var a = 1;

function a(){}

console.log(a);

analysis :

function a(){}

var a;

console.log(a);  ==》 Method body   ƒ a(){}

a = 1;

console.log(a); ==》 1

console.log(a); 

function a(){}

var a = function(){}

analysis :

function a(){}

var a;

console.log(a); ==》ƒ a(){}

a = function(){} 

    <script>
        //  summary :
        //  Pre parsing includes variable promotion and function promotion 

        //  Variable Promotion : Only the lifting belt var The variable of   Declaration of promoted variables 
        //           Can't lift without var The variable of 

        //  Function enhancement : Can only promote function Defined function 
        //           Can improve var a = function(){} Function defined by expression 
        //           But the declaration of functions that can be promoted  

        //  Function promotion takes precedence over variable promotion 
        // eg:
        // var a = 1;
        // function a(){}
        // console.log(a) ===>1

        //  If the function and function have the same name   Then it will cover 
        // eg:
        // function f1(){111}
        // function f1(){222}
        // console.log(f1)  ===>222

        //  If the function and variable repeat     that 
        //  branch 2 In this case 
        //  situation 1: The output statement is after 
        // var a = 1;
        // function a(){}
        // console.log(a) ===>1

        //  situation 2: The output statement precedes 
        // console.log(a) ===> function a(){}
        // var a = 1;
        // function a(){}
        // console.log(a) ===>1


        //  Case practice 
        // (1) No var Variables don't increase 
        // console.log(a);
        // a = 0;

        // (2) belt var Will improve 
        // console.log(a);    
        // var a = 0;
        // console.log(a); 

        // (3)
        // console.log(a);
        // var a = ' I am a variable. ';
        // function a() { console.log(' I'm a function ') }
        // console.log(a);  

        //  Improve the results 
        // function a() { console.log(' I'm a function ') }
        // var a;
        // console.log(a);
        // a = ' I am a variable. ';
        // console.log(a);  

        // (4)
        // console.log(a);
        // a++;
        // console.log(a);
        // var a = ' I am a variable. ';
        // function a() {
        //     console.log(' I'm a function ')
        // }
        // console.log(a);


        //  The parsed code 
        // function a() {
        //     console.log(' I'm a function ')
        // }
        // var a;
        // console.log(a);
        // a++;
        // console.log(a);
        // a = ' I am a variable. ';
        // console.log(a);

        // (5)
        // console.log(a);
        // var a = 0;
        // console.log(a);
        // function fn() {
        //     console.log(a);
        //     var a = 1;
        //     console.log(a);
        // }
        // fn()
        // console.log(a);

        //  The result of pre parsing 
        // function fn() {
        //     var a;
        //     console.log(a);
        //     a = 1;
        //     console.log(a);
        // }
        // var a;
        // console.log(a);
        // a = 0;
        // console.log(a);
        // fn()
        // console.log(a);

        // (6)
        console.log(a);
        var a = 0;
        console.log(a);
        function fn() {
            console.log(a);
            a = 1;
            console.log(a);
        }
        fn()
        console.log(a);

        //  Pre parsing results 
        // function fn() {
        //     console.log(a);
        //     a = 1;
        //     console.log(a);
        // }
        // var a;
        // console.log(a);
        // a = 0;
        // console.log(a);
        // fn()
        // console.log(a);
    </script>

 IIFE-- Named functions

Have a clear function name Function of It's called a named function Also known as famous functions

When called Just write the function name directly

function f1(){

                console.log(11);

            }

f1();

IIFE-- Simple anonymous functions

var sum = function(){

                console.log(2222);

            }

sum();

IIFE-- Anonymous functions

IIFE Immediate execution function 、 Anonymous function self call

            (function(){

                console.log(1111);

            })();

            IIFE Structure

            !function(){

                console.log(2222);

            }();

            ~function(){

                console.log(3333);

            }();

            +function(){

                console.log(4444);

            }();

            -function(){

                console.log(5555);

            }();

 IIFE-- effect

        <script>

            // var name = 'zs';

            // var name = 'ls';

            // console.log(name);

            // If you want your own plug-in Or anonymous function self call   Be careful

            // Write a semicolon first

            // ;

            // (function(){

            //     var name = 'zs';

            //     console.log(name);

            // })();

            // (function(){

            //     var name = 'ls';

            //     console.log(name);

            // })()

        </script>

Advanced control of function parameters

        <script>

            console.log(f1.length);         

            function f1(a,b){

                //  Get the number of arguments , Can only be used inside a function    Because outside the function 

                //  I don't know which function argument I got 

                console.log(arguments.length);

                //  Using function objects length Property can get the number of formal parameters 

                //  Can be used anywhere 

                console.log(f1.length);

            }

            f1(1,2,3,4);

        </script>

Advanced control of function parameters -arguments Pseudo array of

        <script>
            function f1(){
                console.log(arguments.length);

                console.log(arguments[0]);
                console.log(arguments[1]);
                console.log(arguments[2]);
                console.log(arguments[3]);
                console.log(arguments[4]);

            }

            f1(1,2,3,4,5);
        </script>

Enter a set of numbers , The function of averaging

        <script>
            // (1) Enter a set of numbers , The function of averaging 
            function avg(){
                var sum1 = 0;
                for(var i = 0; i < arguments.length; i++){
                    sum1 += arguments[i];
                }
                console.log(sum1 / arguments.length);
            }           
            avg(1,2,3,4,5);
        </script>

Execute different logic according to different parameters

        <script>
            //  If it is 2 Parameters   It takes so long to calculate the difference 
            //  If it is 3 Parameters   It takes so long to calculate the sum value 

            function mix(){
                if(arguments.length == 2){
                    return arguments[0] - arguments[1];
                }else if(arguments.length == 3){
                    return arguments[0] + arguments[1] + arguments[2];
                }

            }

            var result = mix(5,2);
            console.log(result);

            var result2 = mix(1,2,3);
            console.log(result2);
        </script>

  Calculation 3 The sum of parameters

        <script>
            //  Default values for formal parameters    If you pass a parameter, you don't execute   If there is no delivery, execute 
            function sum(a,b,c = 0){
                return a + b + c;
            }

            // var result = sum(5,2,0);
            // console.log(result);

            var result = sum('5',2);
            console.log(result);
        </script>

The data is specified by the default value of the parameter

        <script>
            function sum(a,b,c){
                // console.log(typeof typeof c);
                // console.log(typeof c == 'undefined');

                c = typeof c == 'undefined' ? 0 : c;

                return a + b + c;
            }

            var result = sum(1,2);
            console.log(result);
        </script>

 callee attribute

            function f1(){

                arguments.callee You can get the function body

                console.log(arguments.callee);

            }

            f1();

Infinite recursion

function f1(){

                console.log(' Brother, how are you ?');

                f1();

            }

            f1();

  Realization 5 The factorial

        <script>
            // 5!     5*4*3*2*1

            // var s = 1;
            // for(var i = 5; i >= 1; i--){
            //     s *= i; 
            // }

            // console.log(s);


            
            function f(n){
                if(n == 1){
                    return 1;
                }
                return n * f(n - 1)
            }

            var result = f(5);
            console.log(result);

        </script>

       

copyright notice
author[Delete your code in the middle of the night·],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204290928191602.html

Random recommended