This question already has answers here:
What is destructuring assignment and its uses?
                            
                                (3个答案)
                            
                    
                3个月前关闭。
        

    

我是javascript新手。我正在看预先编写的代码。我不明白大括号中的内容:
 {constructor({零件,工具,数据库})

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

some code here

functions()
...
...
}

最佳答案

这称为解构。例如



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);

09-19 04:10