js -原型

//创建一个学生类
class Student {
//定义一个构造函数,并且要传入两个参数name ,scores
constructor(name, scores) {
this.name = name
this.cores = scores
}
//this其实就是Student类的实例化
//定义方法,打印的方法
introduce() {
console.log(我是${this.name}, 并且我考了${this.cores})
}
}

//学生
//创建实例,并且传入参数
const student = new Student(‘李小时’, ‘95’)
console.log(‘打印数据’, student);
//调用类中的方法
student.introduce()

console.log(‘**************************************************************************************************’);

1.在实例中 直接输出student 只能查找到name 和scores,,是查找不到introduce()方法的,我们可以在student下面的_proto_属性里面找到introduce(),这个就是隐式原型

2.在原本 Student 类中, 下面存在prototype 属性,我们可以在里面找到introduce()方法,这个就是显式原型

这两个属性指向的对象是一样的,,同一个,也就是student.proto ===Student .prototype
也就是,一个对象的隐式原型,会指向创建这个实例的原来类的显式原型


版权声明:本文为qq_40214451原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。