Ask what the output is 3 1 2

var foo=1;
(function(foo) {
    console.log(foo);

    var foo = 2;
    console.log(this.foo);
    console.log(foo);
})(3);

Why does this code output
clipboard.png

Oct.08,2021

first of all, this is the Ramda function (also known as anonymous self-executing function brother); In this way, if you pass parameter 3 in, it will be executed from top to bottom according to the characteristics of JS single-threaded language, so print out 3 first, and then, uh, your this points to window, so the next print is 1, because you have a global 1, which is basically the result of 2 after you initialize the assignment.


pre-compilation process:
Anonymous. The scope of the function has two parts:
go (global): foo:1;
ao (within the function): variable declaration step: 1. Shape participates in variable promotion and assigns it to undefined, that is, foo=undefined;2. The actual parameter value of the shape parameter is the same, that is, foo=3;3. The internal function declares promotion, and its value is the function itself. (when the variable has the same name as the function, it overrides the variable, and the function declaration promotion is better than the variable declaration promotion)
execution process:
find the foo, from the scope so output 3, and then foo=2 (then the foo in ao equals 2); output this.foo (because this points to window), so output window.a= global aqum1; finally output 2 in ao;

Menu