11. 继承
1. 原型链继承
function Person() {
this.person = "人"
}
Person.prototype.say = function () {
console.log("我是个人");
}
function Student() {
this.student = "学生"
}
Student.prototype = new Person()
const student = new Student()
console.log(student.student);
console.log(student.person);
student.say()
student.person = "学生对象的人"
console.log(student.person)
console.log(student.__proto__.person)
console.log(student)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/11330a45f464c6eafbf769e51998bb80.png#pic_center)
2. call
函数
function A() {
this.name = "jack"
this.age = 20
}
function B() {
this.name = "tom"
}
B.prototype.say = function(){
console.log(this.name)
console.log(this.age)
}
const a = new A()
const b = new B()
b.say.call(a)
3. call
继承(借用构造函数)
function Person(){
this.name = "人"
}
Person.prototype.say = function(){
console.log("我是个人");
}
function Student(){
Person.call(this)
this.age = 12
}
const student = new Student()
const person = new Person()
console.log(student.name)
console.log(student.text)
person.say()
Student.prototype = new Person()
4. 构造函数和原型链继承集结合
function Person(){
this.name = "人"
this.age = 10
}
Person.prototype.say = function() {
console.log("我是"+this.name)
}
function Student(){
Person.call(this)
this.school = "八一中学"
}
Student.prototype = new Person()
const student = new Student()
console.log(student);
student.name = "学生"
student.say()