function Person(name, age) {
this.name = name
this.age = age
this.say = function () {
console.log('Hello World')
}
}
1 构造函数继承/Call 式继承
function Star(name, age) {
Person.call(this, name, age)
}
const s1 = new Star('尼古拉斯.赵四', 18)
console.log(s1.name)
console.log(s1.age)
s1.say()
function Person(name, age) {
this.name = name
this.age = age
this.say = function () {
console.log('Hello World')
}
}
function Star(name, age) {
Person.call(this, name, age)
}
const s1 = new Star('尼古拉斯.赵四', 18)
const s2 = new Star('尼古拉斯.赵六', 19)
console.log(s1.say === s2.say)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
const s1 = new Star('尼古拉斯.赵四', 18)
s1.say()
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
Star.prototype = Person.prototype
Star.prototype.play = function () {
console.log('play')
}
const p1 = new Person()
p1.play()
2 原型继承
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
const p = new Person()
Star.prototype = p
Star.prototype.play = function () {
console.log('play')
}
const s1 = new Star('尼古拉斯1', 18)
const s2 = new Star('尼古拉斯2', 18)
console.log(s1.say === s2.say)
const ppp = new Person()
ppp.play()
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
const p = new Person()
Star.prototype = p
const s1 = new Star('尼古拉斯1', 18)
console.log(Star.prototype.construcor === Star)
console.log(s1.__proto__.construcor === Star)
console.log(s1.construcor === Star)
3组合继承(构造函数+原型继承)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
Star.prototype = new Person()
Star.prototype.construcor = Star
const s1 = new Star('尼古拉斯1', 18)
console.log(Star.prototype.construcor === Star)
console.log(s1.__proto__.construcor === Star)
console.log(s1.construcor === Star)
构造函数继承(继承的是属性)+原型继承(继承的父类原型上的方法) = 组合继承 */
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function ()
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
Star.prototype = new Person()
Star.prototype.construcor = Star
const s1 = new Star('尼古拉斯1', 18) */
4 寄生继承
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
function Temp() { }
Temp.prototype = Person.prototype
const t = new Temp()
Star.prototype = t
Star.prototype.construcor = Star
const s1 = new Star('尼古拉斯1', 18)
console.log(s1)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
function create(Person) {
function Temp() { }
Temp.prototype = Person.prototype
return new Temp()
}
Star.prototype = create(Person)
Star.prototype.construcor = Star
const s1 = new Star('尼古拉斯1', 18)
console.log(s1)
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.say = function () {
console.log('Hello World')
}
function Star(name, age) {
Person.call(this, name, age)
}
function create(Person, Star) {
function Temp() {
this.construcor = Star
}
Temp.prototype = Person.prototype
return new Temp()
}
Star.prototype = create(Person, Star)
const s1 = new Star('尼古拉斯1', 18)
console.log(s1)
- 构造函数继承 => 继承的是属性
- 原型继承 => 继承的是父类构造函数上的方法
- 组合继承 = 构造函数继承 + 原型继承
- 原型继承的问题:子类的.prototype 上有无用属性是 undefined
- 寄生继承,弄一个新方法,把父类.prototype 等于新方法.prototype,然后把新方法的实例给了子类.prototype
- 寄生组合继承 => 构造函数继承 + 寄生继承