


JavaScript study notes (3): JavaScript also has an entrance Main function_javascript skills
In C and Java, there is an entry function or method for the program, that is, the main function or main method. In JavaScript, the program starts running from the head of the JS source file. But in a sense, we can still make up a main function as the starting point of the program. In this way, not only can it be unified with other languages, but maybe you will have a deeper understanding of JS.
1. Actual entrance
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 will create a new scope when executed. Specifically, a new scope will be created when the execution process enters the function, and this scope will be destroyed when the function execution completes and exits. The formal parameters and local variables of the function will be bound to this scope. When the function call completes the scope destruction, they will be destroyed accordingly. Of course, in special cases, if some variables in the scope are still referenced when the function returns, the scope and these referenced variables will not be destroyed, forming a so-called closure.
On the other hand, we know that functions can be nested, so scopes can also be nested. When a function is defined, the JS engine will set a built-in attribute called [[scope]] for 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. Normally, there is only one scope chain at any time, that is, starting from the scope of the executing function and tracing upward layer by layer until the outermost global scope.
[Note]: Functions on the scope chain are functions nested layer by layer in the JS source code. They have nothing to do with the order in which the functions are executed or the function call stack. This is also the origin of the name lexical scope.
The 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 point 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 are always valid.
[Scope of function 3]-->[Scope of function 2]-->[Scope of function 3]-->[Global scope]
In addition, corresponding to the global scope, there is a global object. In the browser, the global object is the window object. The global object is a special object:
Variables defined in the global scope will be bound to the global object.
Variables defined in any scope, if defined without the var keyword, will be bound to the global object.
In the global scope, this points to the global object.
It can be seen from the characteristics listed above that if the global scope is regarded as an object, then it is actually the global object. In addition, this also explains why the following four statements are equivalent in the global scope:
var a = 1; a = 1; window.a = 1; this.a = 1;
3. Imaginary main function
Since they are all scopes, why do we need a special global scope? We always prefer simplicity and consistency and try to avoid complexity and specificity. So naturally, we wonder if we can make the global scope look the same as the function scope? The answer is yes. We can think of this:
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 this main function as the entry point of the program.
That is, suppose there is code like this in a JS file:
var a = 1; var b = 2; function add(x, y) { var z = x + y; return z; } console.log(add(a, b));
The JS engine will wrap the program into a main function before it starts executing:
// 虚构的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 this main function:
main._current_scope_ = window; // 将全局作用域(对象)设为window main.call(window) // 将this指向window
4. What’s the significance?
(1) JS also has the entry function main, which is consistent with other languages.
(2) The concept of global scope is omitted, or the global scope becomes the function scope.
(3) Through the above calling process of the main function, we can understand the origin of the special properties in the global scope.
(4) The last point is to treat all JS source code as a function to pave the way for the event queue and event loop later.
The above are the JavaScript study notes (3) introduced by the editor to you: JavaScript also has the entrance Main function, I hope you like it.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
