Analysis of the immediate execution function of javascript

不言
Release: 2018-07-11 10:07:26
Original
1293 people have browsed it

This article mainly introduces the analysis of javascript's t immediate execution function. It has certain reference value. Now I share it with you. Friends in need can refer to it.

Concept:
Immediate execution As the name suggests, the function is executed immediately after the function is defined.
Function expression method:
Add parentheses () after the function expression to execute the function immediately.

var xmlhttpUtil = function () { function getText(url,callback){ alert("dog"); }();
Copy after login

Anonymous function method:
Anonymous functions are not allowed to appear directly in javascript as a separate statement, so if you want to execute the function immediately,
must make some grammatical changes: Method 1, It assigns an anonymous function to a variable, which becomes a function expression and can be executed immediately by adding parentheses after it.
Method 2: Put a bracket around the anonymous function (this is allowed by js grammar), which is equivalent to a function expression, and then add brackets after it to execute it immediately.

(function(b){ alert(b); })(321);
Copy after login

Modularization:
Because variables inside the function will not pollute the global scope, immediate execution of functions can be used for modularization. Many third-party libraries have adopted this approach.

var xmlhttpUtil = function () { function getText(url,callback){ var request=new XMLHttpRequest(); request.open("GET",url); request.send(null); request.onreadystatechange=function(){ if(request.readyState===4 && request.status===200){ var type=request.getResponseHeader("Content-Type"); if(type.match(/^text/)) callback(request.responseText); alert(request.responseText); } else { } } } let xmlHttpRequestObj={}; xmlHttpRequestObj.getText=getText; return xmlHttpRequestObj; }();
Copy after login

Execution:

xmlhttpUtil.getText("/dog",function () { alert("xmlhttprequest请求响应!"); });
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Algorithmic ideas for implementing quick sort in JavaScript

Introduction to the principle of new calling function in JS

The above is the detailed content of Analysis of the immediate execution function of javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!