A
A
Aleks01234567892020-04-28 12:46:26
JavaScript
Aleks0123456789, 2020-04-28 12:46:26

What is the correct way to pass arguments to a class constructor?

There is such a class:

class Skill {
    constructor({
        typeId,
        name,
        description,
        groupId,
        primaryAttribute, // 180
        secondaryAttribute, // 181
        skillTimeConstant, // 275
        requiredSkill1, // 182
        requiredSkill1Level, // 277
        requiredSkill2, // 183
        requiredSkill2Level, // 278
        requiredSkill3, // 184
        requiredSkill3Level, // 279
    }) {
        this.typeId = typeId;
        this.name = name;
        this.description = description;
        this.groupId = groupId;
        this.primaryAttribute = primaryAttribute; // 180
        this.secondaryAttribute = secondaryAttribute; // 181
        this.skillTimeConstant = skillTimeConstant; // 275
        this.requiredSkill1 = requiredSkill1; // 182
        this.requiredSkill1Level = requiredSkill1Level; // 277
        this.requiredSkill2 = requiredSkill2; // 183
        this.requiredSkill2Level = requiredSkill2Level; // 278
        this.requiredSkill3 = requiredSkill3; // 184
        this.requiredSkill3Level = requiredSkill3Level; // 279
    }
}


What is the best way to pass a large number of arguments?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Klimenko, 2020-04-28
@Aleks0123456789

const options = {
    typeId,
    name,
    description,
    groupId,
    primaryAttribute, // 180
    secondaryAttribute, // 181
    skillTimeConstant, // 275
    requiredSkill1, // 182
    requiredSkill1Level, // 277
    requiredSkill2, // 183
    requiredSkill2Level, // 278
    requiredSkill3, // 184
    requiredSkill3Level, // 279
};

class Skill {
    constructor(options) {
        Object.assign(this, options);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question