admin管理员组文章数量:1318564
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" }
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" }
Share
Improve this question
asked Jan 14, 2020 at 12:35
Ma KobiMa Kobi
9047 silver badges22 bronze badges
1
- There is nothing to be gained from this. It will not make any difference performance wise and it will make it way harder to read. – adiga Commented Jan 14, 2020 at 12:39
2 Answers
Reset to default 12If you want the constructor to accept discrete arguments, there's nothing much better than the individual assignment you're already doing, at least for now.
You could have the constructor accept an object instead, which would let you do this:
constructor(args) {
Object.assign(this, args);
}
which you call like this:
new Component({param1: "arg1", param2: "arg2", param3: "arg3"});
but if it's just one to three parameters, that's creating extra work at every call site rather than once in the constructor code, and the constructor code is less clear.
A third option is to accept a rest parameter:
constructor(...args) {
["param1", "param2", "param3"].forEach((name, index) => {
this[name] = args[index];
});
}
which you call in the normal way, but again while it's subjective, to me readability is impaired.
Just as a side note, as I think you probably know, TypeScript has this feature. It's increasingly mon now to use TypeScript (which piles to JavaScript for use on browsers). TypeScript isn't tied to Angular, you can use it with just about any framework (or none).
In TypeScript, for instance:
// TypeScript
class Component {
constructor(
public param1: string,
public param2: string,
public param3: string
) {
}
}
There's no code necessary in the constructor at all, TypeScript automatically assigns those parameter values to those public properties. (You can also use private
or protected
.)
Since you are passing an object as param, you can use the Object.keys
Sample snippet code
class Component {
constructor(args) {
Object.keys(args).forEach(argKey => this[argKey] = args[argKey])
}
}
let a = new Component({param1: "arg1", param2: "arg2", param3: "arg3"})
console.log(a)
版权声明:本文标题:node.js - How to dynamically map javascript class constructor arguments to instance properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050104a2418020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论