Implement a static method of a class and call it to get how many instances of this constructor / class have been new?

call: ClassA.getInstanceNumber ()

Feb.28,2021

seems to work well with ES6's modifier .

var Test = (function () {
    var count = 0;
    function Test() {
        if (!(this instanceof Test)) {
            return new Test();
        }
        countPP;
    }
    Test.getInstanceNumber = function () {
        return count;
    }
    return Test;
})();

Test();
new Test();
new Test();
console.log(Test.getInstanceNumber());//3
Menu