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.
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.
- 1 Are you willing to restrict the features that classes are allowed to use? What if someone adds logic in the constructor that is critical to the behavior of the class object itself, like binding a function or something? – loganfsmyth Commented Jun 14, 2018 at 14:29
- well there is an implementation problem for which i need this. not sure if this is the right place to talk about it, but the point is that sometimes i need to instantiate the class when i do not have any data, just entity id. and if there would be constructor parameters (which i definitely want to allow), i could not call the constructor because of that -- i know i could call it with null, but then there is the issue with passing null where you are required to pass something -- possibly object, and when user uses property of that object, he would get a null pointer there. – Martin Adámek Commented Jun 14, 2018 at 14:37
- the point is that i want users to use constructor just for creating new entity (and not in other places), this is something that will be documented so i think its fine. – Martin Adámek Commented Jun 14, 2018 at 14:44
- Fair enough. To me this seems like an argument for not using a constructor to set properties like you want, and instead keep the constructor simple and build the other logic outside the constructor. – loganfsmyth Commented Jun 14, 2018 at 16:20
1 Answer
Reset to default 21You can add a static
method create, that create an Object from the class prototype. Something like that should work:
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