js 继承标准方式

来源:5-8 【TS继承准备】借用构造函数+原型链继承组合模式

慕UI9336467

2021-09-05

function Parent(a, b) {
    this.a = a
    this.b = b
}

function Children(a, b, c, d) {
    Parent.call(this, a, b)
    this.c = c
    this.d = d
}

Children.prototype = Object.create(Parent.prototype)
Children.prototype.constructor = Children

Parent.prototype.log = function (msg) {
    console.log(msg)
}

console.log(new Children(1, 2, 3, 4))

这不完了吗?没必要讲太长时间了。

写回答

2回答

keviny79

2021-09-05

同学你可能不清楚手写TS继承底层用的手写的寄生组合模式,并不是单独用Object.create实现寄生组合,所以后面讲了手写的寄生组合模式,底层考虑的是手写的寄生组合扩展性更好

0
0

keviny79

2021-09-05

同学你可能不清楚TS底层用的手写的寄生组合模式,并不是单独用Object.create

0
0

晋级TypeScript高手,成为抢手的前端开发人才

轻松驾驭 TypeScript 高级用法, 突破前端成长瓶颈

871 学习 · 425 问题

查看课程