class Downloader { constructor(data, options) { this.data = data; this.options = options; this.item = null; this.onStart = null; this.onProgress = null; this.onCompleted = null; this.data.progress = 0; // 新增 progress 属性 this.data.status='pause';//true 表示 进度没有结束,继续下载 this.data.timeid=0; } start() { const win = BrowserWindow.getFocusedWindow(); this.options.onStarted = (item) => { this.item = item; this.data.status='process';//true 表示 进度没有结束,继续下载 this.onStart(); }; this.options.onTotalProgress=(progress)=>{ this.data.progress = progress.percent * 100; // 更新 progress 属性 this.onTotalProgress(progress); } this.options.onCompleted = () => { this.onCompleted(); }; download(win, this.data.dowloadLink, this.options); } pause() { if (this.item) { this.item.pause(); } } resume() { if (this.item) { this.item.resume(); } } cancel() { if (this.item) { this.item.cancel(); } } } 这个下载器,里面的方法没有挂载到对象上是什么原因。、
时间: 2024-04-28 09:26:25 浏览: 89
这个下载器中的方法没有挂载到对象上是因为它们是定义在 Downloader 类的原型上的。在 JavaScript 中,类的方法通常都是定义在原型上的,而不是在对象实例上。这样做的好处是可以节省内存,因为所有的对象实例都可以共享同一个方法实现。当我们创建一个类的实例时,JavaScript 引擎会自动将该实例的原型指向该类的原型,因此该实例就可以访问该类的所有方法。
相关问题
// 下载器类 class Downloader { constructor(data, options) { this.data = data; this.options = options; this.item = null; this.onStart = null; this.onProgress = null; this.onCompleted = null; this.data.progress = 0; // 新增 progress 属性 this.data.status='pause';//true 表示 进度没有结束,继续下载 this.data.timeid=0; } start() { const win = BrowserWindow.getFocusedWindow(); this.options.onStarted = (item) => { this.item = item; this.data.status='process';//true 表示 进度没有结束,继续下载 this.onStart(); }; this.options.onTotalProgress=(progress)=>{ this.data.progress = progress.percent * 100; // 更新 progress 属性 this.onTotalProgress(progress); } this.options.onCompleted = () => { this.onCompleted(); }; download(win, this.data.dowloadLink, this.options); } pause() { if (this.item) { this.item.pause(); } } resume() { if (this.item) { this.item.resume(); } } cancel() { if (this.item) { this.item.cancel(); } } } 这是我说的类,里面有异步方法,怎么实现
你可以在异步方法执行完毕后,通过 Promise 或回调函数的方式通知外部代码。例如,在 onCompleted 回调函数中设置一个 Promise,然后在外部代码中等待这个 Promise 执行完毕,再执行下一步操作。类似下面这样:
```
class Downloader {
constructor(data, options) {
this.data = data;
this.options = options;
this.item = null;
this.onStart = null;
this.onProgress = null;
this.onCompleted = null;
this.data.progress = 0;
this.data.status = 'pause';
this.data.timeid = 0;
}
start() {
const win = BrowserWindow.getFocusedWindow();
this.options.onStarted = (item) => {
this.item = item;
this.data.status = 'process';
this.onStart();
};
this.options.onTotalProgress = (progress) => {
this.data.progress = progress.percent * 100;
this.onTotalProgress(progress);
};
this.options.onCompleted = () => {
this.onCompletedPromise.resolve(); // 执行 Promise
};
this.onCompletedPromise = {}; // 保存 Promise 对象
this.onCompletedPromise.promise = new Promise((resolve, reject) => {
this.onCompletedPromise.resolve = resolve;
this.onCompletedPromise.reject = reject;
});
download(win, this.data.dowloadLink, this.options);
}
async pause() {
if (this.item) {
await this.onCompletedPromise; // 等待 Promise 执行完毕
this.item.pause();
}
}
async resume() {
if (this.item) {
await this.onCompletedPromise; // 等待 Promise 执行完毕
this.item.resume();
}
}
async cancel() {
if (this.item) {
await this.onCompletedPromise; // 等待 Promise 执行完毕
this.item.cancel();
}
}
}
```
这样就能保证异步方法执行完毕后再执行外部代码了。
electron-dl 创建了一个类,用户管理多个下载器,但是我无法拿到每个下载器的进度条。 // 下载器类 class Downloader { constructor(data, options) { this.data = data; this.options = options; this.item = null; this.onStart = null; this.onProgress = null; this.onCompleted = null; } start() { const win = BrowserWindow.getFocusedWindow(); this.options.onStarted = (item) => { this.item = item; if (this.onStart) { this.onStart(); } }; this.options.onProgress = (progress) => { if (this.onProgress) { this.onProgress(progress); } }; this.options.onCompleted = () => { if (this.onCompleted) { this.onCompleted(); } }; download(win, this.data.dowloadLink, this.options); } pause() { if (this.item) { this.item.pause(); } } resume() { if (this.item) { this.item.resume(); } } cancel() { if (this.item) { this.item.cancel(); } } }
如果你想要获取每个下载器的进度条,你可以在 Downloader 类中添加一个 progress 属性,用于存储每个下载器的进度。然后在 onProgress 回调函数中更新该属性的值。这样,你就可以通过调用 Downloader 实例的 progress 属性来获取每个下载器的进度条了。以下是修改后的代码示例:
```
class Downloader {
constructor(data, options) {
this.data = data;
this.options = options;
this.item = null;
this.onStart = null;
this.onProgress = null;
this.onCompleted = null;
this.progress = 0; // 新增 progress 属性
}
start() {
const win = BrowserWindow.getFocusedWindow();
this.options.onStarted = (item) => {
this.item = item;
if (this.onStart) {
this.onStart();
}
};
this.options.onProgress = (progress) => {
this.progress = progress; // 更新 progress 属性
if (this.onProgress) {
this.onProgress(progress);
}
};
this.options.onCompleted = () => {
if (this.onCompleted) {
this.onCompleted();
}
};
download(win, this.data.dowloadLink, this.options);
}
pause() {
if (this.item) {
this.item.pause();
}
}
resume() {
if (this.item) {
this.item.resume();
}
}
cancel() {
if (this.item) {
this.item.cancel();
}
}
}
```
阅读全文