ES6創建對象的方法有以下幾種:
const person = { name: 'Alice', age: 20 };
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person = new Person('Alice', 20);
const person = Object.create(null);
person.name = 'Alice';
person.age = 20;
const obj1 = { name: 'Alice' };
const obj2 = { age: 20 };
const person = Object.assign({}, obj1, obj2);
const obj1 = { name: 'Alice' };
const obj2 = { age: 20 };
const person = { ...obj1, ...obj2 };