js创建对象的方式
1.字面量
const obj = {}
2.工厂函数
function createPerson(name, age) {
return {
name,
age,
greet() {
console.log("Hello!");
}
};
}
const person = createPerson("Charlie", 35);
3.Object.create
4.构造函数
cosnt obj = new Object()
5.class
class Person{
constructor(name){
this.name = name
}
}
6.Object.assign
7.构造函数
function createObject(age, name){
this.age = age
this.name = name
}
let obj = new createObject(14,'fxk')