Javascript XmlHttpRequest onload`this`属性
给定下面的代码,Javascript XmlHttpRequest onload`this`属性,javascript,Javascript,给定下面的代码,onChunkComplete方法中的这个就是XHR请求。这是有道理的。有没有办法让它成为实际的上传者对象 function Uploader(file, options) { // ... code here .... this.upload_request = new XMLHttpRequest(); this.upload_request.onload = this._onChunkComplete;
onChunkComplete
方法中的这个就是XHR请求。这是有道理的。有没有办法让它成为实际的上传者
对象
function Uploader(file, options) {
// ... code here ....
this.upload_request = new XMLHttpRequest();
this.upload_request.onload = this._onChunkComplete;
}
Uploader.prototype = {
// ... code here ...
_onChunkComplete: function() {
if (this.range_end === this.file_size) {
console.log('done');
return;
}
this.range_start = this.range_end;
this.range_end = this.range_start + this.chunk_size;
if (!this.is_paused) {
this._upload();
}
}
// ... mode code here...
};
var test = new Uploader(formFile, {});
test.start();
只需通过参数将Uploader
对象传递给函数:
Uploader.prototype = {
// ... code here ...
var uploader = this;
_onChunkComplete: function(uploader) {
// you can use uploader now
if (this.range_end === this.file_size) {
console.log('done');
return;
}
this.range_start = this.range_end;
this.range_end = this.range_start + this.chunk_size;
if (!this.is_paused) {
this._upload();
}
}
// ... mode code here...
};
但是如果您想通过uploader
对象覆盖这个
,那么Martriay的答案是正确的。使用:
this.upload\u request.onload=this.\u onChunkComplete.bind(上传器)
bind()方法创建一个新函数,该函数在调用时具有
此关键字设置为提供的值,并具有给定的
调用新函数时,将在任何函数前面提供参数
我怎么不知道这件事。。。我做了.bind(这个)
,效果非常好。非常感谢。