I would like to create an instance of a class without invoking its constructor.
For example,
class Test {
private greeting: string = "Hello";
constructor() {
console.log("this should not happen");
}
foo() {
console.log(this.greeting + " world!");
}
}
magic({greeting: "Hey"}, Test) instanceof Test // true
For some context, I am working on an ORM. I need this to create JavaScript objects while bypassing the constructor. This is required to create plain-old-objects without the need for factories or parameter-less constructors.
constructor, notconstruct. You could just pass a Boolean argument to it, defaulted to falseconstructor(doFrouFrou = false) { if (doFrouFrou) { console.log('this should happen only if doFrouFrou is true'); } }Thenvar test = new Test();would not log anything... But it looks like you want more than that, because you want to set private variables of this new instance without needing to have an accessor for it. Now that's crazy talk. :)