return this._pool.length === 0
? new Obj(this) // 对象池中没有空闲对象,则创建一个新的对象
: this._pool.shift(); // 对象池中有空闲对象,直接取出,无需再次创建
return this._pool.push(obj);
return this._pool.length;
console.log(`+ 从 ${this.src} 开始下载 ${this.name}`);
console.log(`- ${this.name} 下载完毕`); // 下载完毕后, 将对象重新放入对象池
/****************** 以下是测试函数 **********************/
let objPool = new ObjectPool();
let file1 = objPool.create(File);
file1.src = "https://download1.com";
let file2 = objPool.create(File);
file2.src = "https://download2.com";
let file3 = objPool.create(File);
file3.src = "https://download3.com";
`${"*".repeat(50)}\n下载了3个文件,但其实只创建了${objPool.size()}个对象`