Ajax-Based Anonymous Functions

Source: Internet
Author: User

To use anonymous functions to Implement User existence, you should understand: What is an anonymous function? What is it? How to use it? How can I implement it with Ajax? Then we will go into the study with questions:

1. What is it?

"Anonymous" literally means no name. If you can understand this, you will have half of the understanding of anonymous functions, that is, anonymous functions do not have actual names or pointers. For example:

(Function () {// ignore all implementations here })();

Through this simple anonymous function, we can analyze how it is executed? "()" Is the most common, and "()" is a strange thing. Parentheses can combine our expressions into blocks, and each block, that is, each pair of parentheses, has a return value. The returned value is actually the return value of the expression in parentheses. So when we enclose an anonymous function with a pair of parentheses, in fact, parentheses return the function object of an anonymous function. Therefore, adding an anonymous function to the parentheses is like a function with a name, and we get its reference position. Therefore, if the parameter list is added after the referenced variable, the call form of the common function is implemented.

2. What should I do?

So What problems does it solve? What is the purpose of this writing? JavaScript does not support encapsulation because it is not object-oriented. However, encapsulation can also be implemented in languages that do not support encapsulation, And the implementation method is anonymous functions. It can be seen that its purpose is to generate an encapsulation.

Do you remember the first sentence in Javascript module mode is "global variables are the devil ". With the VaR keyword, the anonymous function can effectively ensure that JavaScript is written on the page without causing global variable pollution. This is very effective and elegant when adding JavaScript to a page that is not very familiar.

3. How to use it?

Do you still remember the previous blog? Can I use ajax to determine whether a user exists? The callback method is used to obtain the data returned by the Ajax engine, so now we use the anonymous Function Method to perform the same operation (Here we only look at the JS Code, The rest remains unchanged ):

Function validate (field) {If (TRIM (document. getelementbyid ("userid"). Value). length! = 0) {var XMLHTTP = NULL; // indicates that the current browser is not IE, such as NS, firefoxif (window. XMLHttpRequest) {XMLHTTP = new XMLHttpRequest ();} else if (window. activexobject) {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");} // The cache solution. timestamp var url =" user_validate.jsp? Userid = "+ trim (document. getelementbyid ("userid "). value) + "& time =" + new date (). gettime (); // set the request method to get, set the request URL, and set the asynchronous submission of XMLHTTP. open ("get", URL, true); // copy the method address to the onreadystatechange attribute. // similar to the phone number, the anonymous function is used to implement the callback function XMLHTTP. onreadystatechange = function () {// Ajax engine status is successful if (XMLHTTP. readystate = 4) {// the HTTP protocol status is successful if (XMLHTTP. status = 200) {// alert ("request successful! ") // If it is null, remove the red span from the IF (TRIM (XMLHTTP. responsetext )! = "") {Document. getelementbyid ("spanuserid "). innerhtml = "<font color = 'red'>" + XMLHTTP. responsetext + "</font>"} else {document. getelementbyid ("spanuserid "). innerhtml = "" ;}} else {alert ("request failed, error code =" + XMLHTTP. status) ;}}// send the set information to the Ajax engine XMLHTTP. send (null);} else {document. getelementbyid ("spanuserid "). innerhtml = "";}}

Of course, there are many ways to implement anonymous functions. I will not give them one by one here. I hope to learn more in the future.

Iv. Comparison

By comparing the implementation of the two Ajax methods to determine whether a user exists, we can see that using anonymous functions can organize the previous three functions into a function, which is easier to implement, and can improve efficiency.

V. Summary

Through the study of anonymous functions, I learned that every knowledge point is closely related. Therefore, do not give up any terms that you do not understand or understand in your future studies.

Ajax-Based Anonymous Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.