Home  >  Article  >  Web Front-end  >  Learn about JS! /+/-/~ function() {/*...*/}() What does it mean?

Learn about JS! /+/-/~ function() {/*...*/}() What does it mean?

coldplay.xixi
coldplay.xixiforward
2020-06-29 17:41:592718browse

Learn about JS! /+/-/~ function() {/*...*/}() What does it mean?

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 */ } ();

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete