Property methods can be with parameters:
Copy Code code 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, the above is not necessarily useful, although it is defined directly in the function signature, if it is not called o.show (' param '), but through other function callbacks, because it is not necessarily necessary for others to call this method, such as when using Ajax
Request.onreadystatechange=function (param) {...}
Or
Request.onreadystatechange=callback;function callBack (param) {...}
is not good, because Ajax at this time does not give you the Param parameters, the correct way to form:
Copy Code code as follows:
Request.onreadystatechange = orgeval;//Wrong approach
Request.onreadystatechange = function (Request, porgname) {//Wrong approach
Orgeval (Request, porgname);
//};
//...
Request.onreadystatechange = function () {//correct approach
Orgeval (Request, Porgname)//Call the callback implementation within the anonymous function and pass directly to the parameter, which uses the closure nature of JavaScript
};
//...
function Orgeval (req, OrgName) {
//...
}
This is done by calling the callback implementation function within the anonymous function and passing in the argument directly.
Ajax pass parameter to onreadystatechange callback function
These days began to learn Ajax, in which they do the test page with the pass parameters to the Xmlhttprequest.onreadystatechange callback function this demand. If Baidu, found that there are a lot of people. The search method is probably this:
Xmlhttp.onreadystatechange=function () {callback (a,b);};
The two parameters of A and B are passed.
Later I found a way, the way to know a lot of people, but I did not search on Baidu, here is written to promote the next slightly.
Copy Code code as follows:
Xmlhttp.a=a;
Xmlhttp.b=b;
Xmlhttp.onreadystatechange=callback;
.
.
function callback ()
{
if (this.readystate==4)
{
A=THIS.A;
b=this.b;
.
}
}
That is, add two attributes to the XMLHTTP object and call the two properties in the callback function directly with this.
In addition to say a little bit of their own experience, XMLHTTP best to write global variables, I initially found that some of the requests did not achieve the goal. It was later found that the XMLHTTP was written in a function, and when that function ran out, the XMLHTTP life cycle ended. Some of these requests are implemented before the end of the lifecycle, and some requests are gone.