[英]How to access method variables from within an anonymous function in JavaScript?
I'm writing a small ajax class for personal use. 我正在编写一个供个人使用的小型ajax类。 In the class, I have a "post" method for sending post requests.
在课堂上,我有一个“发布”方法来发送发布请求。 The post method has a callback parameter.
post方法具有一个回调参数。 In the onreadystatechange propperty, I need to call the callback method.
在onreadystatechange属性中,我需要调用回调方法。
Something like this: 像这样:
this.requestObject.onreadystatechange = function() {
callback(this.responseText);
}
However, I can't access the callback variable from within the anonomous function. 但是,我无法从匿名函数内部访问回调变量。 How can I bring the callback variable into the scope of the onreadystatechange anonomous function?
如何将回调变量纳入onreadystatechange匿名函数的范围?
Here's the full code so far: 这是到目前为止的完整代码:
function request()
{
this.initialize = function(errorHandeler)
{
try {
try {
this.requestObject = new XDomainRequest();
} catch(e) {
try {
this.requestObject = new XMLHttpRequest();
} catch (e) {
try {
this.requestObject = new ActiveXObject("Msxml2.XMLHTTP"); //newer versions of IE5+
} catch (e) {
this.requestObject = new ActiveXObject("Microsoft.XMLHTTP"); //older versions of IE5+
}
}
}
} catch(e) {
errorHandeler();
}
}
this.post = function(url,data,callback)
{
var response;var escapedData = "";
if (typeof data == 'object') {
for (i in data) {
escapedData += escape(i)+'='+escape(data[i])+'&';
}
escapedData = escapedData.substr(0,escapedData.length-1);
} else {
escapedData = escape(data);
}
this.requestObject.open('post',url,true);
this.requestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.requestObject.setRequestHeader("Content-length", data.length);
this.requestObject.setRequestHeader("Connection", "close");
this.requestObject.onreadystatechange = function()
{
if (this.readyState == 4) {
// call callback function
}
}
this.requestObject.send(data);
}
}
Just pass the callback function together with the rest of the arguments 只需将回调函数与其余参数一起传递
this.post = function(url, data, callback) {
...
this.requestObject.onreadystatechange = function() {
if (this.readyState == 4) {
callback(this.responseText);
}
};
...
}
And then 接着
foo.post("foo.html", {foo:"bar"}, function(result){
alert(result);
});
By the way, this is a better way to convert the data into a proper string 顺便说一句,这是将数据转换为正确字符串的更好方法
var q = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
q.push(key + "=" + encodeURIComponent(data[key]));
}
}
data = q.join("&"); //data can now be passed to .send()
encodeURIComponent
is the proper function to use here as encode
will not escape data properly encodeURIComponent
是此处使用的正确函数,因为encode
不会正确转义数据
If you want to get a ready made function for all of this you can take a look here http://github.com/oyvindkinsey/easyXDM/blob/master/src/easyXDM.js#L358 如果您想获得现成的所有功能,可以在这里查看http://github.com/oyvindkinsey/easyXDM/blob/master/src/easyXDM.js#L358
var that = this;
然后在匿名函数中使用that
代替this
。
If callback is a variable in the containing function, it should be in scope. 如果callback是包含函数中的变量,则应在范围内。 If it is not a variable, but is in scope in in the containing function, you may have to do something like
如果它不是变量,但在包含函数的范围内,则可能必须执行类似的操作
var cb = callback;
var xhrRequest = this;
then 然后
cb(xhrRequest.responseText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.