Problems related to local variables in javascript.

        function Person(){}                                    
        
        var person = new Person();//Person
        function fun(){
            var index = 0;
            //personsay
            person.run = function(){
                console.log(PPindex);//index
            }
        }
        
        fun();//fun
        //5personrun
        for (var i = 0;i < 5;iPP) {
            person.run();//12345
        }
    :funindexpersonrunindex

May.24,2021

this is a closure.
when you add a function to person.run , the variable in function is not destroyed, even if it is a variable of an external function.


this is a closure.
gives you an address to learn:
https://developer.mozilla.org. https://developer.mozilla.org.


internal function, referencing the variables of the external function.
about closures: JavaScript scope and closure


references to the person.run function have not disappeared

Menu