1,原型继承
function Peoper(name, age) {
this.name = name
this.age = age
}
function Student(name, age) {
}
Student.prototype = new Peoper("继承姓名", "继承年龄")
var S1 = new Student("张三", 18)
console.log(S1)
输出的结果为 继承姓名 继承年龄
缺点 所有属性的值 只和创建时一致,无法改变
2,冒充继承
通过改变this的指向性实现继承方法
function Peoper(name, age) {
this.name = name
this.age = age
}
Peoper.prototype.move = function () {
console.log(`${this.name}正在走`)
}
Peoper.prototype.eat = function () {
console.log(`${this.name}正在吃`)
}
function teath(name, age) {
Peoper.call(this, name, age)
}
var t1 = new teath("李四", 67)
t1.move()
console.log(t1)
缺点:无法继承原型函数
3、组合继承
function Peoper(name, age) {
this.name = name
this.age = age
}
Peoper.prototype.move = function () {
console.log(`${this.name}正在走`)
}
Peoper.prototype.eat = function () {
console.log(`${this.name}正在吃`)
}
function teath(name, age) {
Peoper.call(this, name, age)
}
teath.prototype = new Peoper()
var t1 = new teath("李四", 67)
t1.move()
console.log(t1)
使用组合继承将两者的优点结合 是最优的解决方法