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;

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3affd-2baaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3affd-2baaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?