一、用途
重复创建对象时,对同一类对象赋予相同的属性和方法。
二、示例
function YangGuang (name){//构造函数
YangGuang.prototype.school = "阳光小学";
this.name = name;
}
class ShiYan {//class类
constructor(name) {
ShiYan.prototype.school = "实验小学";
this.name = name;
}
}
let xiaoMing = new YangGuang("小明");
let xiaoLi = new YangGuang("小丽");
let xiaoMei = new ShiYan("小美");
let xiaoGang = new ShiYan("小刚");
console.log(xiaoMing)//阳光小学的小明
console.log(xiaoLi)//阳光小学的小丽
console.log(xiaoMei)//实验小学的小美
console.log(xiaoGang)//实验小学的小刚