JS继承

继承是为了子类可以使用父类的所有功能,并且能对这些功能进行扩展。

1. 构造函数继承(call&apply)

说明:直接利用call或者apply方法将父类构造函数的this绑定为子类构造函数的this就可以;
缺点:无法继承原型链上的属性与方法;

function Parent1() {
  this.name = 'parent1'
}
Parent1.prototype.say = function () {
    console.log(this.name)
}
function Child1() {
  Parent1.call(this)  //构造函数继承核心代码
  this.type = 'child1'
}
let child1_1 = new Child1()

console.log(child1_1.name) // parent1
console.log(child1_1.say()) // child1_1.say is not a function
image

2. 原型继承

说明:将子类的原型挂载到父类上;
缺点:子类new出来的实例,父类的属性没有隔离,会相互影响;

function Parent2() {
  this.name = 'parent2'
  this.arr = [1, 2, 3]
}
Parent2.prototype.say = function () {
    console.log(this.name)
}
function Child2() {
  this.type = 'child2'
}
Child2.prototype = new Parent2() //原型继承核心代码
let child2_1 = new Child2()
let child2_2 = new Child2()

console.log(child2_1.name) // parent2
console.log(child2_1.say()) // parent2
child2_1.arr.push(4)
console.log(child2_1.arr, child2_2.arr) // [1, 2, 3, 4]  [1, 2, 3, 4]
image

3. 组合继承

说明:组合上面的构造函数与原型继承的功能;
缺点:call()方法已经拿到父类所有的属性 ,后面再使用原型时也会有父类所有属性;

function Parent3() {
  this.name = 'parent3'
  this.arr = [1, 2, 3]
}
Parent3.prototype.say = function () {
    console.log(this.name)
}
function Child3() {
  Parent3.call(this)
  this.type = 'child3'
}
Child3.prototype = new Parent3()

let child3_1 = new Child3()
let child3_2 = new Child3()

child3_1.arr.push(4)
console.log(child3_1.arr, child3_2.arr) //  [1, 2, 3, 4]  [1, 2, 3]
// 缺点是 Parent3执行了两次,child3中有重复属性
image

4. 寄生组合继承

说明:解决组合继承重复属性的问题,直接将子类的原型等于父类的原型,或者是用Object.create继承原型但不执行父类构造函数;
注意处理子类实例的 constructor 指向问题,new Parent2()也有这个问题;

function Parent4() {
  this.name = 'parent4'
  this.arr = [1, 2, 3]
}
Parent4.prototype.say = function () {
    console.log(this.name)
}
function Child4() {
  Parent4.call(this)
  this.type = 'child4'
}

//子类的原型等于父类的原型
Child4.prototype = Parent4.prototype  
//或 Child4.prototype = Object.create(Parent4.prototype)
//修复重写子类原型导致子类constructor属性被修改
Child4.prototype.constructor = Child4

let child4_1 = new Child4()
let child4_2 = new Child4()
child4_1.arr.push(4)
console.log(child4_1.arr, child4_2.arr) // [1, 2, 3, 4]  [1, 2, 3]
console.log(child4_1.constructor === Child4) // true 
console.log(child4_1.constructor === Parent4)// false

// 这里的Child4就是一个继承自Function的函数对象
console.log(Child4.constructor === Parent4) // false
image

5. Class继承

说明:ES6新增,class是一个语法糖,就是基于寄生组合继承来实现的;

class Parent5{
    constructor(){
    this.name = 'Parent5'
    this.arr = [1, 2, 3]
  }
  say(){
    console.log(this.name)
  }
}
class Child5 extends Parent5{
   constructor(){
    super() //通过super()调用父类构造函数
    this.type="Child5"
  }
}
let child5_1 = new Child5()
let child5_2 = new Child5()
child5_1.arr.push(4)
console.log(child5_1.say()) // Parent5
console.log(child5_1.arr, child5_2.arr) // [1, 2, 3, 4]  [1, 2, 3]
console.log(child5_1.constructor === Child5) // true 
console.log(child5_2.constructor === Parent5) // false
image

1.ES6里Class的Extends继承原理
寄生组合式继承原理:
1.使用借用构造函数(call)来继承父类this声明的属性/方法
2.通过寄生式封装函数设置父类prototype为子类prototype的原型来继承父类的prototype声明的属性/方法

  1. 《你不知道的JS》上卷 A.1
    class 很好地伪装成 JavaScript 中类和继承设计模式的解决方案,但是它实际上起到了反作用:它隐藏了许多问题并且带来了更多更细小但是危险的问题。

补充:constructor的作用

constructor属性不影响任何JavaScript的内部属性。instanceof检测对象的原型链,通常你是无法修改的(不过某些引擎通过私有的proto属性暴露出来)。
constructor其实没有什么用处,只是JavaScript语言设计的历史遗留物。由于constructor属性是可以变更的,所以未必真的指向对象的构造函数,只是一个提示。不过,从编程习惯上,我们应该尽量让对象的constructor指向其构造函数,以维持这个惯例。
作者:贺师俊
链接:https://www.zhihu.com/question/19951896/answer/13457869

补充:new与Object.create()区别

  • new创建一个对象,执行构造函数。
  • Object.create相当于创建一个对象,但是不执行构造函数。
function Parent(){
    this.name="Parent"
}
Parent.prototype.age = '18'
let child_new = new Parent()
let child_create = Object.create(Parent.prototype)

console.log(child_new instanceof Parent) // true
console.log(child_create instanceof Parent) // true
console.log(child_new.name,child_new.age) // Parent 18
console.log(child_create.name,child_create.age) // undefined "18"

//简单模拟下 Object.create 如下:
function create(obj) {
  let F = function () {}
  F.prototype = obj
  return new F()
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容