3 回答
TA贡献1794条经验 获得超8个赞
呐,这是es6不用装饰器node和最新chrome可用版
class ClassA {
constructor() {
ClassA.instanceNumber = (ClassA.instanceNumber || 0) + 1;
}
static getInstanceNumber() {
return ClassA.instanceNumber || 0;
}
}
console.log(ClassA.getInstanceNumber());
let a = new ClassA();
console.log(ClassA.getInstanceNumber());
let b = new ClassA();
console.log(ClassA.getInstanceNumber());
TA贡献1827条经验 获得超7个赞
var Test = (function () {
var count = 0;
function Test() {
if (!(this instanceof Test)) {
return new Test();
}
count++;
}
Test.getInstanceNumber = function () {
return count;
}
return Test;
})();
Test();
new Test();
new Test();
console.log(Test.getInstanceNumber());//3
添加回答
举报