JavaScript Learning Notes (iii): JavaScript also has entrance main function _javascript tips

Source: Internet
Author: User

In C and Java, there is a program's entry function or method, which is either the main function or the main method. In JavaScript, the program is run from the head of the JS source file. But in a sense, we can still invent a main function as a starting point for the program, so that not only can be unified with other languages, and perhaps you will have a deeper understanding of JS.

1. The actual entrance

When a JavaScript file is given to the JS engine, the JS engine executes each statement 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 produces a new scope when it executes. Specifically, a new scope is created when the process enters the function, and the scope is destroyed when the function execution completes exiting. The parameters of a function, local variables are bound to this scope, and when the function call completes the scope destroy, they are destroyed. Of course, in special cases, if some variables in the scope are still referenced when the function returns, the scope and the referenced variables will not be destroyed to form the so-called closure.

On the other hand, we know that functions can be nested, so scopes can also be nested. When the function is defined, the JS engine sets a built-in property called [[scope]] to each function, which points to the lexical scope of the external function. In this way, multiple scopes form a chain structure called a scope chain. In general, there is only one scope chain at any time, that is, starting from the scope of the function being executed, the layers are traced back to the outermost global scope.

[Note]: The function on the scope chain is JS source layer nested functions, with the function of the execution of the order or function call stack, which is also the lexical scope of the origin of this name.

A global scope is a special scope that is not a function scope, but it is the outer scope of all function scopes and the end point of all scope chains. So as long as the program does not exit, the global scope always exists, and the variables within the global scope are also valid.

[function 3 scope]-->[function 2 scope]-->[function 3 scope]-->[global scope]

In addition, there is a global object that corresponds to the global scope. In the browser, the global object is the Window object. The global object is a special object:

Variables defined in the global scope are bound to the global object.

Variables defined in any scope, if defined without using the var keyword, are bound to the global object.

In the global scope, this points to the global object.

The features listed above show that if the global scope is treated as an object, then 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. The fictional main function

Since all are scopes, why should there be a special global scope? We always like simplicity, consistency, and try to avoid complications, specificity. So naturally, we wonder if we can make the global scope look different from the scope of the function? The answer is yes. We can do this idea:

We imagine that when the JS engine executes the source file, the code in the file is packaged into a function called Main. This main function is then used as the entrance to the program.
In other words, suppose a JS file has such a 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 wraps the program into a main function before it starts executing:

The fictitious main function 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 this main function:

main._current_scope_ = window; Set global scope (object) to Window
main.call (window)///To point to window

4. What is the point?

(1) JS also has the entry function main, consistent with other languages.

(2) The concept of global scope is omitted, or the global scope becomes a function scope.

(3) through the above to the main function of the call process, you can understand the global scope of the origin of those special characteristics.

(4) The last point, all JS source code as a function, is to talk about the event queue, the event loop to do bedding.

The above is a small series to introduce the JavaScript learning notes (iii): JavaScript also has the entrance main function, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.