I'm building a Node.js app (Node v10.11.0) and want to do it in a OOP-way without using Typescript. So I've built some classes like this:
class Component {
constructor(param1, param2, param3) {
this.param1= param1;
this.param2 = param2;
this.param3 = param3;
}
}
I wondered if there is a way in javascript to map the arguments of the constructor automatically without writing this.param1 = param1 for every single constructor argument.
I already tried running over arguments object with somthing like this:
constructor(param1, param2, param3) {
Array.from(arguments).forEach(arg => {
this[arg] = arg;
}
}
but this doesn't work. The result of this approach is:
console.log(new Component("arg1", "arg3", "arg3"))
// Component { arg1: "arg1", arg2: "arg2", arg3: "arg3" }
// I want to have this:
// Component { param1: "arg1", param2: "arg2", param3: "arg3" }