The correct method for passing ajax callback function parameters

Source: Internet
Author: User

The property method can contain parameters:
Copy codeThe Code is as follows:
Function ClassX (name ){
This. name = name;
ClassX. prototype. show = function (param ){
Alert (this. name + "" + param );
};
}
Var o = new ClassX ("name ");
O. show ("param"); // name param

However, although the above is a reference defined directly in the function signature, if you do not call o. show ('param') is not necessarily useful when it is passed in through callback of other functions, because when someone else calls this method, it is not necessarily passed in to you, for example, when using ajax

Request. onreadystatechange = function (param ){...}

Or

Request. onreadystatechange = callBack; function callBack (param ){...}

This is difficult, because ajax does not pass the param parameter to you at this time. The correct method is as follows:
Copy codeThe Code is as follows:
// Request. onreadystatechange = orgEval; // Error Method

// Request. onreadystatechange = function (request, pOrgName) {// Error Method
// OrgEval (request, pOrgName );
//};

//...
Request. onreadystatechange = function () {// correct practice
OrgEval (request, pOrgName); // call the callback implementation in the anonymous function and directly pass in the parameter. The closure nature of JavaScript is used here.
};
//...

Function orgEval (req, orgName ){
//...
}

In this way, you can call the callback implementation function in an anonymous function and directly input parameters.

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.