As we all know, in c and java, the main function provides the main method as the entry function or method of a program. Starting from the header of the JS source file in js, we can still create a main function as the starting point of the program, so that it can be integrated with other languages, and you may have a deeper understanding of JS. If you are interested, you can learn it with xiaobian. In C and Java, there is a program entry function or method, that is, the main function or main method. In JavaScript, the program runs from the header of the JS source file. However, in a sense, we can still create a main function as the starting point of the program, so that it can be integrated with other languages, and you may have a deeper understanding of JS.
1. Actual Portal
When a JavaScript file is handed over to the JS engine for execution, the JS engine executes each statement one by one from top to bottom until all the code is executed.
2. Scope chain, global scope, and Global Object
We know that every function in JS generates a new scope during execution. Specifically, a new scope will be created when the execution flow enters the function, and this scope will be destroyed when the function execution is completed and exited. The parameters and local variables of the function are bound to this scope. When the function call completes the destruction of the scope, they are destroyed. Of course, in special circumstances, if some variables in the scope are still referenced when the function returns, the scope and these referenced variables will not be destroyed, thus forming a so-called closure.
On the other hand, we know that functions can be nested, so the scope can also be nested. When defining a function, the JS engine sets a built-in attribute called [scope] for each function, pointing to the lexical scope of the external function. In this way, Multiple scopes form a chain structure called the scope chain. In general, there is only one scope chain at any time, that is, from the scope of the function being executed to the global scope of the outermost layer.
[Note]: The function on the scope chain is a layer-by-layer nested function in the JS source code. It has nothing to do with the order of function execution or the function call stack. This is also the origin of the lexical scope.
Global scope is a special scope. It is not a function scope, but it is the outer scope of all function scopes and the end of all scope chains. Therefore, as long as the program does not exit, the global scope always exists, and the variables in the global scope remain valid.
[Function 3 Scope] --> [function 2 Scope] --> [function 3 Scope] --> [global scope]
In addition, there is a global object corresponding to the global scope. In the browser, the global object is the window object. A global object is a special object:
Variables defined in the global scope are bound to global objects.
Variables defined in any scope are bound to global objects if the var keyword is not used during definition.
In the global scope, this points to the global object.
From the features listed above, we can see that if the global scope is regarded as an object, it is actually a global object. In addition, this explains why the following four statements are equivalent in the global scope:
var a = 1;a = 1;window.a = 1;this.a = 1;
3. Fictitious main Function
Since they are all scopes, why is there a special global scope? We always like simplicity and consistency, and try to avoid complexity and particularity. So naturally, we will wonder if the global scope looks no different from the function scope? The answer is yes. We can make the following ideas:
We imagine that when the JS engine executes the source file, it will wrap the code in the file into a function called main. Then, use the main function as the program entry.
That is to say, assume that a JS file contains the following code:
var a = 1;var b = 2;function add(x, y) { var z = x + y; return z;}console.log(add(a, b));
The JS engine packs the program into a main function before it starts execution:
// Fictitious main function main () {var a = 1; var B = 2; function add (x, y) {var z = x + y; return z ;} console. log (add (a, B ));}
Then, call the main function:
Main. _ current_scope _ = window; // set global scope (object) to windowmain. call (window) // point this to window
4. What is the significance?
(1) JS also has the entry function main, which is consistent with other languages.
(2) the concept of global scope is removed, or the global scope also becomes the function scope.
(3) through the calling process of the main function, we can understand the special nature of the global scope.
(4) The last point is to regard all JS source code as a function to pave the way for subsequent discussions on Event queues and event loops.
The above is the JavaScript Study Notes (3) introduced by the editor. JavaScript also has the Main function, and I hope you will like it.