Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is there a main function in javascript?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "there is no main function in javascript". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "there is no main function in javascript".

There is no main function in javascript. The main function is often used in Java and C language, also known as the main function, it requires a return value, the return of 0 represents the normal execution of the program, and the return of a non-zero value represents the abnormal end of the program, but the function does not exist in JavaScript.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

Does javascript have a main function?

Javascript has no main function

Brief introduction

The main function, also known as the main function, as the only entry for most C programs, requires a return value, which is returned to (such as the operating system) to indicate the execution status of the program. Returning 0 means that the program executes successfully, and a value other than 0 means that the program ends abnormally, so the return value needs to be an int integer, so there is the specification of int main ().

If you use void main (), that is, to declare that the main function has no return value, the program can compile and run successfully, but it is not conducive to the activator of the program to judge its state, which can be fatal for large projects composed of many C programs.

Especially from the C99 standard (the second edition of the official C language standard developed in 1999), you must use int main (). If you don't add return 0; language, C99 requires the compiler to add it automatically (it's a good habit to write it yourself). If only main () is declared, the system defaults to int main (). Void main () should never be used, because the main function must have a return value indicating the running state of the program (it is a good habit not to use void main () in your code).

Define

In the latest C99 standard, only the following two definitions are correct:

Int main (void) int main (int argc, char * argv []) / / char * argv [] can be written as char * * argv

Parameters.

Void: do not accept any parameters

Argc: the number of parameters passed to the program on behalf of the environment in which the program is running

Argv: a pointer to the first element of an array of argc+1 pointers. The elements at the end of the array are null pointers, and if preceded by elements, they point to a string that represents the parameters passed to the program from the host environment. If argv [0] is not a null pointer (or argc > 0), it points to a string that represents the name of the program. The string is empty if the program name is not available from the host environment.

Return value

The return value is used as a parameter to implicitly call exit (), with values of 0 and EXIT_SUCCESS indicating successful termination, and values of non-0 and EXIT_FAILURE indicating unsuccessful termination.

The example is as follows: make up a main function in JavaScript

In both C and Java, there is a program entry function or method, that is, the main function or the main method. In JavaScript, the program starts at the header of the JS source file. But in a sense, we can still invent a main function as the starting point of the program, so that not only can it be unified with other languages, but you may have a better understanding of JS.

1. Actual entrance

When a JavaScript file is given 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.

two。 Scope chains, global scopes, and global objects

We know that each function in JS produces a new scope when it is executed. Specifically, a new scope is established when the execution process enters the function, and the scope is destroyed when the function execution finishes exiting. The formal parameters and local variables of the function are bound to this scope, and when the function call finishes destroying the scope, they are destroyed. Of course, in special cases, if some variables in the scope are still referenced when the function returns, then the scope and these referenced variables will not be destroyed, thus forming the 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 sets a built-in property 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 scope chain. In general, there is only one chain of scope at any time, that is, starting from the scope of the function being executed, layer by layer up to the outermost global scope.

[note]: the functions on the scope chain are layers of nested functions in the JS source code, regardless of the order in which the functions are executed or the function call stack, which is the origin of the term 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 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, 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 the global object.

Variables defined in any scope that are not defined with the var keyword are bound to the global object.

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

As can be seen from the features listed above, if a global scope is treated 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 = 1x a = 1x window.a = 1world this.a = 1

3. Fictitious main function

Since they are all scopes, why should there be a special global scope? We always like simplicity and consistency, while trying to avoid complexity and particularity. So naturally, we wonder if we can make the global scope look no different from the function scope? The answer is yes. We can make the following idea:

We imagine that when the JS engine executes the source file, it wraps the code in the file into a function called main. Then use this main function as the entry to the program.

That is, suppose there is code like this in a JS file:

Var a = 1bot var b = 2; function add (x, y) {var z = x + y; return z;} console.log (add (a, b))

The JS engine wraps the program as a main function before it starts execution:

/ / 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 the main function:

Main._current_scope_ = window; / / set the global scope (object) to windowmain.call (window) / / point this to window

4. What's the point?

(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 has become a functional scope.

(3) through the process of calling the main function, we can understand the origin of those special properties in the global scope.

(4) finally, all the JS source code is regarded as a function, which is to pave the way for event queues and event loops.

Thank you for your reading, the above is the content of "there is no main function in javascript". After the study of this article, I believe you have a deeper understanding of whether there is a main function in javascript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report