Original blog :https://blog.csdn.net/yaojxing/article/details/72784774

1. Common scenarios for anonymous functions

js Anonymous functions in are a very common type of functions , More common scenarios :

  1. <input type="button" value=" Click on " id="btn">
  2. <script type="text/javascript">
  3. // The first case of anonymous functions
  4. var btn=document.querySelector("#btn");
  5. btn.onclick=function(){
  6. // alert("aaaaa");
  7. }
  8. // The second case of anonymous functions
  9. setInterval(function(){
  10. // alert("bbbbb");
  11. }, 1000);
  12. // The third case of anonymous functions
  13. var fun=function(){
  14. alert("ccccc");
  15. }
  16. // fun();
  17. // The fourth case of anonymous function
  18. var obj={
  19. name:"dddd",
  20. say:function(){
  21. alert(this.name);
  22. }
  23. }
  24. obj.say();
  25. </script>

The above shows the common use scenarios of anonymous functions .( Be careful :querySelector yes H5 New search in dom Element method )

2. Anonymous self executing function

See the name, think of the meaning , An anonymous self executing function is, first of all, an anonymous function , But this function is self executing , No need for other elements .
  1. <input type="button" value=" Click on " id="btn">
  2. lt;script type="text/javascript">
  3. //1, The first way to implement anonymous functions
  4. (function(data){
  5. // alert(data);
  6. })("eee");
  7. //2. The second implementation of anonymous self executing function
  8. (function(){
  9. // alert("fff");
  10. }());
  11. //3. The third way to implement anonymous self executing functions
  12. !function(data){
  13. // alert(data);
  14. }("hhh");
  15. //4. The fourth way to implement anonymous self executing functions
  16. var fun=function(data){
  17. alert(data);
  18. }("iii");
From the above code block, we can conclude that there are four ways to implement anonymous self executing functions .

3. The role of anonymous self executing functions

1. The most common use of anonymous self executing functions is in the case of closures . I'll explain the concept of closure in detail in the following blog . Here's a brief description of closures . Closure : Closure is js A characteristic of , We can connect the inside and outside of a function through closures , And it can make the local variables of function always exist in memory .
 
2. Anonymous self executing functions can also be used in js Create a block level scope in the simulation , That is, if you use anonymous self executing functions to wrap some code, you can achieve the effect of block level scope , Reduce the number of global variables , After the anonymous self executing function is executed, the variable will be released by memory , And that saves memory .
 

4, Summary of anonymous functions and anonymous self executing functions

Anonymous functions can be simply understood as functions without names , The common scenes are 4 Kind of .
Anonymous self executing functions can be simply understood as anonymous functions that can be executed by themselves , There are several ways to implement anonymous self executing functions 4 Kind of .
The function of anonymous self executing function is used for closure and creating independent namespace .

JavaScript Anonymous self executing function ~function(){} More articles about

  1. understand javascript The immediate execution function in (function(){})()

    I saw a lot of code before , It's useful to write this function , But I didn't seriously think about why I wrote like this , Today I want to learn jquery Source code , Find out jquery It's the same way , use (function(window, undefine ...

  2. javascript The immediate execution function in (function(){…})()

    javascript The immediate execution function in (function(){…})() In depth understanding of javascript The immediate execution function in , Immediate function is also called immediate function , Usually it's written in (function(){…})() package ...

  3. js Advanced use of closures in anonymous self executing functions (---------------------------******-----------------------------)

    Let's first look at the most common problem : <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. js Anonymous functions and anonymous self executing functions in

    1. Common scenarios for anonymous functions js Anonymous functions in are a very common type of functions , More common scenarios :   <input type="button" value=" Click on " id ...

  5. js Anonymous self executing function

    Anonymous self executing function : Function closures without method names : A closure is a function that has access to another function scope variable : Explain with an example : I found a case on the Internet , Used for loop . Anonymous self executing function .setTimeout. Case study 1: va ...

  6. JavaScript Function declaration , Function expression , The difference between anonymous functions , Deep understanding of immediate execution functions (function(){…})()

    function fnName(){xxxx}; // Function declaration : Use function Keyword declares a function , In specifying a function name . // for example :( normal , because promote The function declaration , Function calls can precede function declarations ) f ...

  7. javascript Immediate execution function (function(){})()

    See a piece of code : (function(){ var outer = $('#subject'); outer.find('li').on('mouseover', mouseover); })() ( ...

  8. In depth understanding of javascript The immediate execution function in (function(){…})()

    contribute :junjie typeface :[ increase Reduce ] type : Reprint Time :2014-06-12 I want to comment on This article mainly introduces the in-depth understanding of javascript The immediate execution function in , Immediate function is also called immediate function , Usually it's written as ...

  9. Javascript The basic concept of function + Anonymous immediate execution function

    Function declaration . Function expression . Anonymous functions Function declaration :function fnName () {…}; Use function Keyword declares a function , Specify another function name , It's called function declaration . Function expression var fnName = f ...

Random recommendation

  1. iOS 2D Drawing details (Quartz 2D) And Transform(CTM,Translate,Rotate,Scale)

    Preface :Quartz By default, device independent user space To draw , When context( Drawing board ) After establishment , The default coordinate system origin and direction are also confirmed , Can pass CTM(current transformation ...

  2. pptp vpn

    webalizer It's an efficient . Free of charge web Server log analyzer . The results are as follows HTML File format save , So it's very convenient to pass through web Server browsing ; http://daliang1215.iteye.com/ ...

  3. Teach you to remember ASP.NET WebForm The life cycle of a page

    about ASP.NET Webform The developer of the , understand ASP.NET Webform The life cycle of a page is very important . The main purpose is to find out where to place specific methods and when to set various page properties . But the memory and understanding page life cycle provides ...

  4. 03C# Basics (2)

    1. Comparison operator == be equal to ;  != It's not equal to ;  > Greater than ;  >= Greater than or equal to ;  < Less than ;  <= Less than or equal to ; Comparison operator ( Also known as relational operators ) It's used to judge whether it's true or false , The result is boo ...

  5. hdu5323 Solve this interesting problem( Explosive search )

    Reprint please indicate the source : http://www.cnblogs.com/fraud/          ——by fraud Solve this interesting problem Time Limit ...

  6. GCD Asynchronous synchronization experience

    The previous blog post also wrote about synchronous and asynchronous , It might be reading other people's blogs , I don't have an experiment , I don't understand very well , So I typed some code to compare the serial . Parallel corresponding synchronization . asynchronous . 1. First, create a serial . Parallel thread queue 1 2 dispatch_queu ...

  7. spring mvc Cross domain solutions for

    What is cross-domain In a word : The same ip. The same network protocol . Same port , All three are satisfied in the same domain , Otherwise, it's cross domain . Why cross domain Based on two aspects : a. web The application itself is deployed on different servers b. From a development perspective -- ...

  8. wet download jdk 64 position

    wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com% ...

  9. Python3 The fifth in the actual combat series ( Get India after sales data project )

    problem : Continue with the previous one . Let's do it , Write the program diligently ! The goal is : In this article, we try to put python The program is packaged into .exe Program . So you can run it on the server . The second step to realize the first plan list function module : 2. take python Program to ...

  10. Pairing projects junit The test case

    subject : Let's say we're going to write a class of integer division , And write him test cases . Pairing division of labor : Teng Juan is responsible for writing the code , Data collection , Arrangement , Pan Guangmei is responsible for the test , Processing test results github Address Links : https://github.com/ ...