The function object in js is a fascinating thing, but because it is too flexible, it is often confusing. I will post some code below:
Most people abbreviate it like this:
function test () {}
The entire book "JSVAScript Language Essence" Write like this:
var test = function () {}
The function can be run and assigned immediately:
var test = function () {} () // test === undefined
var test2 = function () {return 'sugar cake'}() // test2 === 'sugar cake'
But the function abbreviation cannot be run directly. The following code will report an error:
function test() {}() // SyntaxError: syntax error
If wrapped with the "()" operator, it is normal:
(function test () {})();
In fact, the function name test has no meaning , remove it and it becomes an anonymous function, which can still automatically execute the code in the function body. Commonly used anonymous function writing methods:
(function () {})();
Anonymous functions can also be written like this, which may be more "beautiful":
(function () {}());
See this, new to it Is Brother JS going crazy? I once saw a guy who was working on the C language in my project become petrified immediately after seeing anonymous functions...