gpt4 book ai didi

javascript - XmlHttpRequest onload `this` 属性

转载 作者:行者123 更新时间:2023-11-30 15:52:09 25 4
gpt4 key购买 nike

给定下面的代码,_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com