Js closure scope

recently, when I was looking at the design pattern, I encountered a problem about closures. I can"t figure it out. Ask for help. The code is as follows:

function foo(){
    var a = 10;
    function bar(){
        a*= 2;
        return a;
    }
    return bar;
}

var baz = foo();
console.log(baz.toString());


var blat = foo();
console.log(blat.toString());

console.log(baz==blat)

printed result:

clipboard.png

:


:

clipboard.png

aaccbazblat
Jul.30,2021

var baz = foo();
var blat = foo();
returnbaz = foo();
return

the first paragraph of code does not point to the same reference. If you call foo, twice, each call will define a new bar function


baz and blat don't point to the same reference, do you


pay attention to the difference in performing assignments, although I can't explain it very well, if anyone makes sense to you, Remember to tell me about


Brother. The closure you first wrote returns the function body, but you don't write a function body. The two are not comparable at all.

Menu