Calling ES6 class constructor from class static method

Furman

I'm trying to do my implementation of singleton pattern in JS ES6 class. Here is what I wrote so far:

let instance;

export class TestClass{

    constructor(){
        if(new.target){
            throw new Error(`Can't create instance of singleton class with new keyword. Use getInstance() static method instead`);
        }
    }
    testMethod(){
        console.log('test');
    }
    static getInstance(){
        if(!instance) {
            instance = TestClass.constructor();
        }

        return instance;
    }
}

However, when I call static method TestClass.getInstance(), I'm not getting instance of class object, I'm getting

ƒ anonymous() {

}

function, without access to testMethod. I can't find error in my code - help will be greatly appreciated.

Bergi

TestClass is the constructor function. TestClass.constructor is the builtin Function, which when called constructs a new empty function (what you are logging).

The TestClass constructor can also be accessed as TestClass.prototype.constructor, that's what you probably meant:

static getInstance(){
    if (!instance) {
        instance = TestClass.prototype.constructor();
    }
    return instance;
}

This will of course throw an exception that you cannot call class constructors without new.

You also should simplify to new TestClass. Or even better, in case you want to support subclassing, new this - notice that this in static method refers to the class (constructor) itself.

I'm trying to do my implementation of singleton pattern in JS ES6 class

Please don't. Singletons are bad practice. If your class doesn't have any state, and there's just one instance anyway, don't use a class. Just write

export function testMethod() {
    console.log('test');
}
// Yes, that's the whole file!

If you insist on lazily constructing the module, I would recommend

let instance;
/*default*/ export function getInstance() {
    return instance || (instance = { // use a simple object literal
        testMethod(){
            console.log('test');
        }
    });
}

That said, if you insist on making a "private" constructor I would pass a token:

const internal = Symbol("creation token for TestClass");
export class TestClass {
    constructor(token) {
        if(token !== internal) {
            throw new Error("Please use the TestClass.getInstance() static method instead");
        }
    }
    …
    static getInstance(){
        return new TestClass(internal); // make sure not to call `this`, otherwise subclassing could leak the token
    }
}

But you should never really need that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling a class method from the constructor

From Dev

Calling constructor of parent class in static method of child class

From Dev

Calling a constructor from method within the same class

From Dev

Is constructor a static method of a class?

From Dev

Javascript calling class within class constructor (ES6)

From Java

Calling static method on a class?

From Dev

calling static method from inside the class

From Dev

es6 access static method of child class from static method of parent?

From Dev

How to return non static variable from static method ES6 Class

From Dev

TS/ES6: Instantiate class without calling constructor

From Dev

Calling a method from a class

From Dev

Create mockImplementation for es6 Class' Static Method in Jest

From Dev

calling a constructor from a derived class

From Dev

Calling constructor of inherited class with only static methods

From Dev

How to refer overriden static method from parent class constructor?

From Dev

Mock static method called from constructor in java class

From Dev

Calling a static jar class method from command line

From Dev

Calling a non-static method from outside the class

From Dev

Trouble calling a non-static method from an abstract class

From Dev

Calling non-static method from another file/class

From Dev

Access to a ES6 / ES7 static class variable within class and constructor

From Dev

Calling up functions from another class in es6

From Dev

Ruby class with static method calling a private method?

From Dev

How to create array of objects of ES6 class while calling constructor of this class?

From Dev

JS & ES6: Access static fields from within class

From Dev

calling a java constructor of same class from non static context leads to recursion but with static it works fine?

From Dev

Calling a class's method as default arg in constructor

From Java

Calling virtual method in base class constructor

From Dev

Unity - Inject an object to Constructor by calling a Method of a Class

Related Related

  1. 1

    Calling a class method from the constructor

  2. 2

    Calling constructor of parent class in static method of child class

  3. 3

    Calling a constructor from method within the same class

  4. 4

    Is constructor a static method of a class?

  5. 5

    Javascript calling class within class constructor (ES6)

  6. 6

    Calling static method on a class?

  7. 7

    calling static method from inside the class

  8. 8

    es6 access static method of child class from static method of parent?

  9. 9

    How to return non static variable from static method ES6 Class

  10. 10

    TS/ES6: Instantiate class without calling constructor

  11. 11

    Calling a method from a class

  12. 12

    Create mockImplementation for es6 Class' Static Method in Jest

  13. 13

    calling a constructor from a derived class

  14. 14

    Calling constructor of inherited class with only static methods

  15. 15

    How to refer overriden static method from parent class constructor?

  16. 16

    Mock static method called from constructor in java class

  17. 17

    Calling a static jar class method from command line

  18. 18

    Calling a non-static method from outside the class

  19. 19

    Trouble calling a non-static method from an abstract class

  20. 20

    Calling non-static method from another file/class

  21. 21

    Access to a ES6 / ES7 static class variable within class and constructor

  22. 22

    Calling up functions from another class in es6

  23. 23

    Ruby class with static method calling a private method?

  24. 24

    How to create array of objects of ES6 class while calling constructor of this class?

  25. 25

    JS & ES6: Access static fields from within class

  26. 26

    calling a java constructor of same class from non static context leads to recursion but with static it works fine?

  27. 27

    Calling a class's method as default arg in constructor

  28. 28

    Calling virtual method in base class constructor

  29. 29

    Unity - Inject an object to Constructor by calling a Method of a Class

HotTag

Archive