Self-executing anonymous function:
- Common format: (function() { /* code */ })();
- Explanation: The first pair of brackets surrounding the function (function(){}) returns an unnamed function to the script, and then a pair of empty brackets immediately executes the returned unnamed function. Inside the brackets are the parameters of the anonymous function.
- Function: You can use it to create a namespace. As long as you write all your code in this special function package, it will not be accessible from the outside unless you allow it (add window before the variable, so that the function or the variable becomes global). The code of each JavaScript library is basically organized in this form.
To summarize, the main functions of the execution function are anonymous and automatic execution. The code is already running when it is interpreted.
Other writing methods
(function () { /* code */ } ());
!function () { /* code */ } ();
~function () { /* code */ } ();
-function () { /* code */ } ();
+function () { /* code */ } ();
Copy after login
Related learning tutorials: javascript tutorial
The above is the detailed content of Learn about JS! /+/-/~ function() {/*...*/}() What does it mean?. For more information, please follow other related articles on the PHP Chinese website!