将父类的对象实例赋值子类的原型。
原型链继承的查找路线:对象实例--->对象的构造函数----->对象的原型----->父类实例------>父类构造函数---->父类的原型
function Person(name, age) {
this.name = name;
this.age = age;
}
//方法
Person.prototype.say = function () {
console.log(this.name + " 在哈哈大笑");
}
//学生对象
function Student(no, name, age) {
this.no=no;
}
//将父类的对象实例赋值给子类的原型对象
Student.prototype = new Person();//原型链继承
Student.prototype.study = function () {
console.log(this.name + " 在奋笔疾书....");
}
// var s = new Student();
// console.log(s);
// s.say()
var s = new Student(1000,'小刚',6);
console.log(s);
s.say()
本文介绍了JavaScript中的原型链继承机制,通过实例展示了如何将父类的对象实例赋值给子类的原型,以及如何调用继承的方法。内容包括Person类和Student类的定义,以及say()和study()方法的实现。在创建Student实例后,调用了say()方法来展示原型链的查找过程。

被折叠的 条评论
为什么被折叠?



