1

I've tried the basic use of "this". However, despite being natural coming from OOP I still found it verbose. Imagine that I had 1000 properties: I'd be in trouble.

class Question{
constructor(number, point, beginning, A, B, C){
    this.number = number;
    this.grade = grade;
    this.beginning = beginning;
    this.A = A;
    this.B = B;
    this.C = C;
}

}

So is there any way to assign properties to Javascript's objects with less code?

8
  • I can't think of a language wherein N named properties can be assigned in fewer than N statements. Commented Jan 7, 2021 at 2:28
  • 2
    If you are using typescript then parameter properties might also be an option: typescriptlang.org/docs/handbook/… Commented Jan 7, 2021 at 2:29
  • 1
    if you had a 1000 properties on 1 object you'd have bigger problems :) Commented Jan 7, 2021 at 2:31
  • Do you want something similar to copy constructor? If the values are coming from another Question object (or any other object with the desired properties), you can loop through the properties and assign to "this" using indexer [] syntax. Right? Commented Jan 7, 2021 at 2:35
  • I just wanted the Question object to have multiple properties. If I pass an object instead, shouldn't it be the same after manipulating it inside methods? Commented Jan 7, 2021 at 2:37

1 Answer 1

4

While you could use Object.assign and shorthand properties:

constructor(number, point, beginning, A, B, C){
    Object.assign(
        this,
        {
            number,
            grade,
            beginning,
            A,
            B,
            C,
        }
    );
}

Once you start to get a lot of parameters, it can be difficult for the caller to understand what's going on. Eg:

new Question(8, 5, 2, 's', 33, 9)

It may be quite unclear from the caller what all those properties refer to. You could pass a single object as a parameter instead, which both makes it clear from the caller's perspective which properties correspond to which value, and makes the constructor much more concise:

const q = new Question({
    number: 8,
    grade: 5,
    // ...
});
constructor(props) {
    Object.assign(this, props);
}

All that said, if your Question really doesn't have anything other than the constructor, I'd omit the class entirely and use a plain object literal; the class isn't providing any benefit.

const q = {
  number: 8,
  grade: 5,
  // ...
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! By the way, in the last case u've mentioned. How can I insert functions in that object? Like one property, func: fn().
func() { /* do stuff */ } ?
Yes. I'll manipulate those properties and I need functions for it. Thats why I chose to encapsulate them in a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.