0

I am new to javascript. I am looking at pre-written code. I do not understand what is in the curly brackets: {constructor({parts, tools, database})

class MyMenu {
  constructor({parts, tools, database}) 
 {
     ...
    this.database = database;   
    this.tools = this._myTools(tools);
    this.parts = this._myParts(parts);
    ..
 }

some code here

functions()
...
...
}
hamid
  • 17
  • 4

3 Answers3

1

This is called destructuring. e.g.

const obj = { a: 1, b: 2 };

// you can get value of a using destructuring like this

const { a } =  obj;

console.log(a);

// Similarly, this applies to function arguments

//e.g.

const personData = { firstName: 'John', lastName: 'Doe', age: 20 };


class Person {
   constructor ({ firstName, lastName, age }) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
   }
}

const person = new Person(personData);

console.log(person.firstName);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

This is object destructuring: https://2ality.com/2015/01/es6-destructuring.html#simulating-named-parameters-in-javascript

Investigate paragraph 2.3.3.

Viacheslav Yankov
  • 988
  • 10
  • 19
0

This is destructuring, and it is an awesome feature!

o = {key1: 'value1', key2: 'value2'}
const {key2} = o
console.log(key2)

It is essentially a way of pulling elements out of objects, without having to traverse the whole object.

Greg
  • 1,845
  • 2
  • 16
  • 26