es6 destructering

We want to pass a settings object to the constructor of the Data class.

The settings object:

{
  id: 1,
  name: 'John'
}

Without destructering:

class Data{
  constructor(settings){
    this.id = settings.id;
    this.name = settings.name;
  }
}

With destructering:

class Data{
  constructor(settings){
    ({id: this.id, name: this.name} = settings);
  }
}

0 Responses to “es6 destructering”


  • No Comments

Leave a Reply