TS/ES6: Instantiate class without calling constructor

Martin Adámek

Is there any way how to instantiate new class instance without calling its constructor?

Something like this:

class Test {
    constructor(foo) {
        this.foo = 'test';
    }
}

const a = new Test('bar'); // call constructor
const b = Test.create();   // do not call constructor
console.log(a.foo, a instanceof Test); // bar, true
console.log(b.foo, b instanceof Test); // undefined, true

I am trying to develop TS mongo ORM, and would like to use constructors of entities for creating new objects, but do not want to call them when instancing entities of already persisted objects (those that are already stored in DB).

I know that doctrine (PHP ORM) uses this approach, but afaik they are using proxy classes to achieve it. Is there any easy way to achieve this in typescript (or generally in ES6/ES7)?

I already found this question ES6: call class constructor without new keyword, that asks for the opposite, and saw one answer mentioning Proxy object. That sounds like a possible way to go, but from the docs I am not really sure if it is achievable.

ZER0

You can add a static method create, that create an Object from the class prototype. Something like that should works:

class Test {
  constructor(foo) {
    this.foo = foo;
  }
  static create() {
    return Object.create(this.prototype);
  }
}

const a = new Test('bar'); // call constructor
const b = Test.create();   // do not call constructor
console.log(a.foo, a instanceof Test); // bar, true
console.log(b.foo, b instanceof Test); // undefined, true

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instantiate .NET class without public constructor

From Dev

Byte Buddy instantiate class without parameters for constructor

From Java

Is there a way to instantiate a class without calling __init__?

From Dev

Constructing a class in python without directly calling the constructor

From Dev

Calling base class' constructor without defining it in derived class

From Dev

Difference Between Class with constructor and Class without when calling a function?

From Dev

Instantiate a Class - With or Without Parens?

From Dev

Initialise class without instantiate?

From Dev

How to instantiate a class with base in the constructor?

From Dev

Python - Inherit an Already Initialized Class Without Calling Super Constructor

From Dev

Inheritence problem in C# without calling base class constructor

From Dev

Calling Constructor with in constructor in same class

From Java

Calling Super class Constructor

From Dev

How to instantiate base class as derived class in constructor?

From Dev

Js es6 class constructor function run before the constructor instantiate

From Dev

Javascript calling class within class constructor (ES6)

From Dev

Calling ES6 class constructor from class static method

From Dev

calling constructor of a class in another class

From Java

Instantiate Interface without Implementation Class

From Dev

How to instantiate a class with an internal constructor in Kotlin

From Dev

How do I instantiate a class with a constructor?

From Java

Instantiate a class object with constructor that accepts a string parameter?

From Dev

Instantiate non-template class with templated constructor

From Dev

Instantiate collection of object from class constructor

From Dev

How to instantiate trait which extends class with constructor

From Dev

Reflectively instantiate class with constructor which takes Enum

From Dev

how to instantiate a class with parameters outside constructor?

From Dev

Instantiate a Raku class, and update an instance variable in the constructor

From Dev

Android: can't instantiate class : no empty constructor

Related Related

  1. 1

    Instantiate .NET class without public constructor

  2. 2

    Byte Buddy instantiate class without parameters for constructor

  3. 3

    Is there a way to instantiate a class without calling __init__?

  4. 4

    Constructing a class in python without directly calling the constructor

  5. 5

    Calling base class' constructor without defining it in derived class

  6. 6

    Difference Between Class with constructor and Class without when calling a function?

  7. 7

    Instantiate a Class - With or Without Parens?

  8. 8

    Initialise class without instantiate?

  9. 9

    How to instantiate a class with base in the constructor?

  10. 10

    Python - Inherit an Already Initialized Class Without Calling Super Constructor

  11. 11

    Inheritence problem in C# without calling base class constructor

  12. 12

    Calling Constructor with in constructor in same class

  13. 13

    Calling Super class Constructor

  14. 14

    How to instantiate base class as derived class in constructor?

  15. 15

    Js es6 class constructor function run before the constructor instantiate

  16. 16

    Javascript calling class within class constructor (ES6)

  17. 17

    Calling ES6 class constructor from class static method

  18. 18

    calling constructor of a class in another class

  19. 19

    Instantiate Interface without Implementation Class

  20. 20

    How to instantiate a class with an internal constructor in Kotlin

  21. 21

    How do I instantiate a class with a constructor?

  22. 22

    Instantiate a class object with constructor that accepts a string parameter?

  23. 23

    Instantiate non-template class with templated constructor

  24. 24

    Instantiate collection of object from class constructor

  25. 25

    How to instantiate trait which extends class with constructor

  26. 26

    Reflectively instantiate class with constructor which takes Enum

  27. 27

    how to instantiate a class with parameters outside constructor?

  28. 28

    Instantiate a Raku class, and update an instance variable in the constructor

  29. 29

    Android: can't instantiate class : no empty constructor

HotTag

Archive