skip to Main Content

I have a few questions regarding the code below:

export class Person {
    constructor(obj = { id: 0, name: '', lastName: '', eyeColor: '', age: 0 }) {
        this.id = obj.id;
        this.name = obj.name;
        this.lastName = obj.lastName;
        this.eyeColor = obj.eyeColor;
        this.age = obj.age;
    }

    yearOfBirth() {
        return new Date().getFullYear() - this.age;
    }

    json() {
        return JSON.stringify(this);
    }
}

FWIW: I prefer to create an object using this syntax:

let newPerson = new Person(
  { 
    id: 0, 
    name: "Werner", 
    lastName: "Venter", 
    age: 37
  }
);

I really dislike this way:

let newPerson = new Person(0, "Werner", "Venter", 37);

It just reads easier for me when I have the parameter name with the value. Anyway, I digress…

  1. Is this the correct/accepted way to create an object class? I will be using it to receive data from an API that sends it in this format as JSON.
  2. How do I make a parameter optional? Mainly why I need this is that the ID value is not required when creating a new entry on the API, so I would prefer not to include the ID value when creating the object.
  3. Finally, do you have any opinion/advice on this approach?

Thank you in advance guys. Really appreciate this community.

3

Answers


  1. Is this the correct/accepted way to create an object class? I will be using it to receive data from an API that sends it in this format as JSON.

    Yes, you have created a class which handles some properties of an object, in this way you must pass a class to the constructor. (because you’ve used statements such as obj.name)

    How do I make a parameter optional? Mainly why I need this is that the ID value is not required when creating a new entry on the API, so I would prefer not to include the ID value when creating the object.

    You just did, by declaring obj = <some_object>, note that by what you did here, in case no object will be passed when you create a new instance of Person, the default object you have provided will be used as a default value.

    let newPerson = new Person(0, "Werner", "Venter", 37);

    This one is wrong btw, your constructor method excepts one parameter while in the example above 4 are provided.

    This way is the correct one indeed (but it drag an error because eyeColor was not provided):

    let newPerson = new Person(
    {
    id: 0,
    name: "Werner",
    lastName: "Venter",
    age: 37
    }
    );

    Login or Signup to reply.
  2. For question 2:

    constructor(obj = { id: 0, name: '', lastName: '', eyeColor: '', age: 0 }) {
        this.id = "unassigned"; // whatever default value
        Object.assign(this, obj); // all properties in obj get written to this, if id exists it will overwrite "unassigned"
    }
    
    Login or Signup to reply.
  3. constructor({ id = 0, name =  '', lastName = '', eyeColor = '', age = 0 } = {}) {
        this.id = id;
        this.name = name;
        this.lastName = lastName;
        this.eyeColor = eyeColor;
        this.age = age;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search