js继承


// 原型链继承 想让Child继承Person的方法,因为Child是可以调用原型Child.prototype上的方法和属性的,所以可以将Person中的方法放在Child的原型上
function Person(name) {
this.name = ‘aaaa‘;
this.say = function () {
console.log(‘say‘)
}
}
function Child() {
this.age = ‘bbb‘
}
Child.prototype = new Person() // 可以将Person中的方法放在Child的原型上,实现了原型链继承
var per = new Child();
per.say()

// 借用使用构造函数继承
function Child2() {
Person.call(this)
}
var per2 = new Child2();
per2.say()
// 组合继承 原型链+构造函数
function Child3() {
Person.call(this)
}
Child3.prototype = new Person()
var per3 = new Child3()
per3.say()
另外还有原型式继承,寄生式继承,寄生组合式继承

相关文章