function is the identifier of JS
function is a constructor in JS
1. New function and new function difference
The new operator in JS is an instance of creating a custom object or an instance of a local object with a constructor function.
Syntax: New constructor [([arguments])]
New function () {...} Initializes an actionable object, equivalent to the new function Anonymous () {...}
The new function (' argument ', ' function body ') instantiates a function that is equivalent to the function name () {...}
2. Execute functions immediately (function () {...} ()) and (function () {...}) ()
//1.//define a named function A and call it, run normallyfunctionA () {}a ()//2.//2What about the way you use anonymous functions? function (){}() //This method will error: uncaught syntaxerror:unexpected token (//3.//according to the above-thought test, to find the cause of 2 errorfunction () {}//3will error, same as 2 error: uncaught syntaxerror:unexpected token (//4.();//4will error: uncaught syntaxerror:unexpected token)//5.(1);//5 No error, normal operation
6.
(function () {});
6 no error, normal operation
It can be seen that the reasons for the above phenomenon are: grammatical errors. The reason for this is that the () operator has precedence over the function call, and the () operator must have an expression inside the parentheses, or there will be a syntax error.
You can therefore define a self-invoking anonymous function in the form of (function () {}) ()
//1.(1+2) (5+6);//in the case of the above expression, an error will be made: Uncaught typeerror:3 is not a function, which means that if the first expression is a function, it will run normally. //2, so in the case of ensuring that the entire operation is an expression operation, the first subexpression is a function, the following wording is obtained(function(){}());//2 normal operation will not error//3, it can be concluded that the following wording is also correct!function(){};+function(){};-function(){};~function(){};......
function and function in JS