组合使用构造函数模式与原型模式(最常用)
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function(){
console.log(`My name is ${this.name}`);
}
Person.prototype.sayAge = function(){
console.log(`My age is ${this.age}`);
}
const person1 = new Person('小刘先森', 26) // 名字与年龄均为本作者 哈哈
person1.sayName(); // 输出:My name is 小刘先森
person1.sayAge(); // 输出: My age is 26
const person2 = new Person('小刘儿', 26)
person2.sayName(); // 输出:My name is 小刘儿
person2.sayAge(); // 输出: My age is 26