给定下面的代码,_onChunkComplete
方法中的this
是XHR 请求。这是有道理的。有没有办法让它成为实际的 Uploader
对象?
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();
最佳答案
使用bind
:
this.upload_request.onload = this._onChunkComplete.bind(Uploader)
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
关于javascript - XmlHttpRequest onload `this` 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39153608/