function Person(name,age){
let o = new Object();
o.name = name;
o.age=age;
o.say=function(){
console.log(this.name)
}
return o;
}
let person1 = Person('chen',18);
console.log(person1 instanceof Person)//false 不能识别对象类型
//然而,工厂模式虽然解决了创建多个相似对象的问题,但是没有解决对象识别问题,即不能知道一个对象的类型
function PersonCon(name,age){
this.name = name;
this.age = age;
this.say=()=>{
console.log(this.name)
}
}
let person2 = new PersonCon('chen',18);
console.log(person2 instanceof PersonCon);
//构造函数不需要写return 可以识别对象类型; 然而它的实例不能被共享。
function PersonPro(name,age){
PersonPro.prototype.name = 'chen';
PersonPro.prototype.age = 18;
PersonPro.prototype.say = ()=>{
console.log(this.name)
}
}
let person3 = new PersonCon('chen',18);
console.log(person3 instanceof PersonCon);
//但是引用类型值,就会出现问题了
function PersonZuhe(name){
this.name =name;
this.girlFirends = ['mimi','kiki'];
}
PersonZuhe.prototype.say = function(){
console.log(this.name)
}
let person7 = new PersonZuhe('kevin');
let person8 = new PersonZuhe('kevin2');
// 这是使用最为广泛、认同度最高的一种创建自定义类型的方法。它可以解决上面那些模式的缺点
// 使用此模式可以让每个实例都会有自己的一份实例属性副本,但同时又共享着对方法的引用
// 这样的话,即使实例属性修改引用类型的值,也不会影响其他实例的属性值了
console