In JavaScript. How can you add all the fields from an object to the fields of the class?

  • 0
    I have a class that only takes one object in its constructor. For convenience, I would like not to manually write down, enter the object's fields by some algorithm or method. How can this be done? Here's an example:
    class A {
        constructor(config) {
            this.a = config.a
            this.b = config.b
            ...
            // чем это можно заменить?
        }
    }
    JavaScript Joshua Jennings, May 29, 2020

  • 1 Answers
  • 0
    for (let item in config) this[item] = config[item];
    Declan Freeman

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!