JS Anonymous Function and Anonymous Self-execution Function Concept and Usage Analysis

The instance of this article tells the JS anonymous function and anonymous self-execution function concept and usage. Share to everyone for your reference, as follows:

1. Frequent scenes of anonymous functions

JS is a very common function type, comparison Common scenes:

// The first case of anonymous function var btn = document.queryselector (“# btn”); btn.onclick = function () {// alert (“aaaaa”);} // The second case of anonymous function setInterval (“bbbbb”);}, 1000); // Anonymous function Three cases var fun = function () {Alert (“ccccc”);} // fun (); // Anonymous function of the fourth situation var Obj = {Name: “dddd”, SAY: function () {Alert (}} Obj.SAY ();
   The usage scene shown above is a common anonymous function. (Note: QuerySelector is a new way to find DOM elements in H5)    2. Anonymous self-execution function 
See the names, anonymous self-execution function first It is an anonymous function, but this function can be automated automatically, do not need to use other elements.

// 1, the first implementation of the anonymous function (DATA) {// Alert (DATA);}) (“eee”); // 2. The second implementation of anonymous self-execution function (“FUNCTION () {// Alert (” fff “);} ()); // 3. Anonymous The third implementation of the function! Function (data) {// alert (data);} (“hHH”); // 4. Anonymous self-execution function VAR fun = function (data) { Alert (DATA);} (“III”); From the above code block we can summarize the method of implementing anonymous self-execution function, there are general four.

3. Anonymous self-execution function

1. The most common role of anonymous self-execution function is used to implement a closure. Concepts about the closure I will introduce in detail in the following article. Here is a brief explanation. Closure: Closure is a characteristic of JS, we can achieve the connection inside the function by closing the package, and the local variable of the function can always exist in memory.
 2. Anonymous self-execution functions can also be used to simulate the creation of block-level scopes in JS, that is, if you use an anonymous self-execution function to encode some code, you can implement the effect of block-level scope, reduce global The number of variables, the variable will be released by the memory after anonymous self-execution function, which will save memory.   4. Anonymous function and anonymous self-execution function summary   
Anonymous function can be simply understood as a function without names, and there are 4 in common scenes.
Anonymous self-execution functions can be easily understood to be able to implement anonymous functions you can implement, there are 4 ways to achieve an anonymous self-execution function.

The role of anonymous self-executing function is to closet and create independent naming spaces.

More about JavaScript Related Contents View this Topic: “JavaScript Common Function Skills Summary”, “JavaScript is object-orientedDoor tutorial, “JavaScript Find Algorithm Skill Summary”, “JavaScript Error and Debug Skill Summary”, “JavaScript Data Structure and Algorithm Skill Summary” and “Summary of JavaScript Mathematical Computing”

I hope this article will be JavaScript programming is helpful.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment