我有一个简单的类来处理Fetch响应的可读流:
class ProgressIndicator {
constructor(res) {
this.res = res;
this.contentLength = parseInt(res.headers.get('content-length', 10));
this.loaded = 0;
return new Response(
new ReadableStream({
start(controller) {
console.log(this);
this.reader = this.res.body.getReader();
this.readStream(controller);
}
})
);
}
很显然,如果我将其记录在启动函数中,则会得到自己的函数。
关于如何将ProgressIndicator对象绑定到start函数的任何想法?
答案 0 :(得分:0)
create属性,它将引用类范围。
class ProgressIndicator {
var selfScope = this;
constructor(res) {
this.res = res;
this.contentLength = parseInt(res.headers.get('content-length', 10));
this.loaded = 0;
return new Response(
new ReadableStream({
start(controller) {
console.log(selfScope);
this.reader = this.res.body.getReader();
this.readStream(controller);
}
})
);
}