我有以下代码,它按预期工作,但我不相信这是正确的方法:
'use strict';
class Upload {
static uploadCount = 20;
constructor(props) {
Upload.uploadCount++;
}
static get count() {
return this.uploadCount;
}
}
const up1 = new Upload({
name: 'Wow.pdf',
size: 123123,
});
const up2 = new Upload({
name: 'doit.pdf',
size: 9999999,
});
console.log(Upload.count);
为什么不能像静态 getter 那样constructor
引用静态变量?this.uploadCount
是否有更好/正确的方法来跟踪实例化Upload
对象的数量?