There is no main function in javascript. The main function is often used in Java and C languages. It is also called the main function and is required to have a return value. Returning 0 means that the program is successfully executed normally, and returning a non-0 value means that the program ends abnormally; but this function does not exist in JavaScript.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
javascript does not have a main function
Introduction
main function, and Called the main function, as the only entry point for most C programs, it is required to have a return value. The return value is returned to (such as the operating system) to indicate the execution status of the program. Returning 0 means that the program is successfully executed normally, and returning a non-0 value means that the program ends abnormally. Therefore, the return value needs to be an integer, so there is the specification of int main().
If you use void main(), it means that the main function has no return value. Although the program can be compiled and run successfully, it is not conducive for the activator of the program to judge its status. This is not suitable for large projects consisting of many C programs. It can be fatal.
Especially starting from the C99 standard (the second edition of the official C language standard formulated in 1999), int main() must be used. If the return 0; language is not added, C99 requires the compiler to automatically add it (write it yourself. good habits). 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 to indicate the running status of the program (it is a good habit not to use void main() in your code).
Definition
In the latest C99 standard, only the following two definitions are correct:
int main(void) int main(int argc, char *argv[]) // char *argv[]可以写成char **argv
Parameters
void: does not accept any parameters;
argc: represents the number of parameters passed to the program by the environment in which the program is running;
argv: a pointer to the first element of the array of argc 1 pointers. The last element of the array is a null pointer, and if there are previous elements, they point to strings representing 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 representing the program name. This string is empty if the program name is not available from the host environment.
Return value
The return value will be used as a parameter for the implicit call to exit(). Values 0 and EXIT_SUCCESS indicate successful termination, and values other than 0 and EXIT_FAILURE indicate unsuccessful termination.
The example is as follows: Make up a main function in JavaScript
In C and Java, there is an entry function for the program Or method, that is, main function or main method. In JavaScript, the program runs 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 code is executed.
2. Scope chain, global scope and global object
We know that every function in JS will generate 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 function being executed 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.
在任意作用域中定义的变量,如果定义时没有用 var 关键字,都会绑定到全局对象。
在全局作用域中, this 指向全局对象。
从上面列举的这些特性可以看出,如果把全局作用域当成一个对象的话,那么实际上它就是全局对象。另外,这也解释了在全局作用域中,下面的四条语句为什么是等价的:
var a = 1; a = 1; window.a = 1; this.a = 1;
3. 虚构的main函数
既然都是作用域,为什么要有一个特殊的全局作用域呢?我们总是喜欢简单化、一致性,而尽量避免复杂化、特殊性。所以很自然地,我们会想能否让全局作用域看起来跟函数作用域没什么区别?答案是肯定的。我们可以做这样的构想:
我们想象,在JS引擎执行源文件时,会将文件中的代码包装到一个叫做main的函数中。然后把这个main函数作为程序的入口。
也就是说,假设一个JS文件中有这样的代码:
var a = 1; var b = 2; function add(x, y) { var z = x + y; return z; } console.log(add(a, b));
JS引擎在程序开始执行前会把它包装成一个main函数:
// 虚构的main函数 function main() { var a = 1; var b = 2; function add(x, y) { var z = x + y; return z; } console.log(add(a, b)); }
然后,调用这个main函数:
main._current_scope_ = window; // 将全局作用域(对象)设为window main.call(window) // 将this指向window
4. 意义何在?
(1) JS也有了入口函数main,跟其他语言一致了。
(2) 省去了全局作用域的概念,或者说全局作用域也成了函数作用域。
(3) 通过上面对main函数的调用过程,可以明白全局作用域中的那些特殊性质的由来。
(4) 最后一点,将所有JS源码当成一个函数,是为了后面讲事件队列、事件循环做铺垫。
相关推荐:javascript学习教程
The above is the detailed content of Does javascript have a main function?. For more information, please follow other related articles on the PHP Chinese website!