//字面量方式1,直接给定属性值 let person = { name="tom" } //字面量方式2,通过get,set方法操作对象 let person = { name, getName: function () { return this.name; }, setName: function (name) { this.name = name; } }; person.setName("tom");
function person(name,age){ this.name=name; this.age=age; } let person3=new person("tom",14); console.log(person3);
//工厂模式 function personFactory(name,age){ let o =new Object(); o.name=name; o.age=age; return o; } let person4=personFactory("jack",16); console.log(person4);
//class 关键字 class User{ constructor(name){ this.name=name; } getName(){ return this.name; } } let user=new User("rose"); console.log(user);
原文:https://www.cnblogs.com/liyanglin/p/13276713.html