谈一谈js中创建对象的方式

20 阅读1分钟

一、字面量

let obj = {}

二、构造函数

function Person(name,age){
   this.age = age
   this.name = name
}

const person = new Person('lisi',18)

d64045cea3c9787d3d2800356bf8b323.png

三、class

class Person{
				
	constructor(name,age){
		this.name = name
		this.age = age
	}
				
}
			
const person = new Person('lihua',20)
console.log(person);

b586bcc5981eb0d1d08e819f7da82aea.png

四、Object.create()