This question is the same: [How do I call one constructor from another in Java?, but JavaScript.
I read that JavaScript does not support multiple declarations of constructors in a class, Is it true?
So, Can I implement the following piece of code?
class A {
constructor(a,b){
console.log([a,b]);
}
constructor(obj) {
console.log(obj.a,obj.b);
}
}
let x1 = new A(2,4);
let x2 = new A({a:2,b:4});
Coming back the original question:
class A {
constructor(a,b){
console.log(a+b);
}
constructor(obj) {
this(obj.a,obj.b);
}
}
let x1 = new A(2,4);
let x2 = new A({a:2,b:4});
If not, What would the most "transparent" design pattern be to do it?