The function object in js is a fascinating thing, but it is often confusing because it is too flexible. I will post some code below:
Most people are abbreviated as follows:
Copy codeThe Code is as follows: function test (){}
The entire book jsvascript is written as follows:
Copy codeCode: var test = function (){}
Functions can be run immediately and assigned values:
Copy codeThe Code is as follows:
Var test = function () {} () // test = undefined
Var test2 = function () {return 'sugar browsed '} () // test2 = 'sugar browsed'
However, function abbreviations cannot be run directly. The following code reports an error:
Copy codeCode: function test () {} () // SyntaxError: syntax error
If the package is normal after the "()" operator is used:
Copy codeThe Code is as follows: (function test (){})();
In fact, this function name "test" is meaningless. After it is removed, it becomes an anonymous function, and the code in the function body can still be automatically executed. Commonly used anonymous function Syntax:
Copy codeThe Code is as follows: (function (){})();
The write of anonymous functions may also be "beautiful:
Copy codeThe Code is as follows: (function (){}());
Seeing this, is it crazy to get started with JavaScript? I once saw my C-language developer in my project. I met anonymous functions and immediately became petrochemical...