几种传统的继承模式和比较

传统形式的继承 ----- > 原型链

如下代码是 father 、 me 和 son 三者之间的继承关系,me 继承自father,son 继承自 me,代码如下

function Father(){
  
}
Father.prototype.lastName = 'han'

function Me(){
  
}
Me.prototype = new Father()

function Son(){
  this.name = 'hehe'
}
Son.prototype = new Me()
var son = new Son()
console.log(son.lastName)  // han
Son.prototype.lastName = 'liu'
console.log(son.lastName)  // liu
console.log(son.name)      // hehe

上面的代码表明

  1. 子代的对象可以继承它父辈们的属性和方法,并且,修改其中某个构造函数自身的属性,不会影响到父辈们身上的属性
  2. 当子代对象需要它原型链上的某个属性时,它先在自身查找,自身没有这个属性,会沿原型链一级一级地向上查找,直至找到为止。
  3. 子代对象会继承他的所有父辈们的所有属性和方法

借用其他构造函数的继承方式

如下代码有两个构造函数,其中一个函数完全涵盖了另一个构造函数的所有方法

function Person(name, age, sex){
  this.name = name
  this.age = age
  this.sex = sex
}
function Student(name, age, sex, grade){
  this.name = name
  this.age = age
  this.sex = sex
  this.grade = grade
}

如果再写一次这个Student构造函数,比较麻烦,而且有很多重复,这时可以借用Person构造函数
代码如下:

function Person(name, age, sex){
  this.name = name
  this.age = age
  this.sex = sex
}
function Student(name, age, sex, grade){
  Person.call(this, name, age, sex)
  this.grade = grade
}
var s1 = new Student('han', 12, 'male', 10)
console.log(s1) // {name: "han", age: 12, sex: "male", grade: 10}

一个函数在 使用new方法的时候:相当于隐式的产生了这样的过程:

function Person(name, age, sex){
  var _this = {
    __proto__: Person.prototype
  }
  _this.name = name
  _this.age = age
  _this.sex = sex
  return _this
}
var p1 = Person('han', 12, 'male')

上面使用 call ,就是在指定 Person 函数内的this必须是我这个Person函数将要构造出的对象
这样写的优缺点:

  • 优点:减少了代码的重复
  • 缺点:增加了性能损耗,因为增加了一个函数的调用

而且它并没有继承构造函数的原型

共享原型

这种方法比较简单粗暴
代码如下

function Father(){}
Father.prototype.name = 'han'

function Me(){}

Me.prototype = Father.prototype

var me = new Me()
console.log(me.name) // han

直接让Me.prototype = Father.prototype ,Me 就可以继承Father的原型上的属性了

但是,因为Me.prototype 和 Father.prototype 是一个对象,也就是说是一个引用值,它们都指向同一个内存空间,修改其中一个,另一个也会变

function Father(){}
Father.prototype.name = 'han'
var father = new Father()
console.log(father.name) // han
function Me(){}

Me.prototype = Father.prototype
Me.prototype.name = 'liu'
var me = new Me()
console.log(father.name) // liu
console.log(me.name)     // liu

古老的圣杯模式

因为直接让两个元素的原型相等,会使得它们的
原理是做一个中间层,不让两个对象之间直接赋值

代码如下

function Father(){}
Father.prototype.name = 'han'
var father = new Father()
// 开始转化
function F(){}
F.prototype = Father.prototype
var f = new F()
Me.prototype = f
// 结束转化
function Me(){}
var me = new Me()
Me.prototype.age = 12
console.log(father.age)  // undefined
console.log(me.age)      // 12

把这个过程做成一个函数

function Father(){}
Father.prototype.name = 'han'
var father = new Father()

function inherit(Target, Origin){
  function F(){}
  F.prototype = Origin.prototype
  Target.prototype =  new F()
}

inherit(Me, Father)
function Me(){}
var me = new Me()
Me.prototype.age = 12
console.log(father.age)  // undefined
console.log(me.age)      // 12

因为 new 出来的是一个新的对象,有不同的内存地址,所以不会出现上面的情况
这样可以做到既可以有父辈的属性和方法,改变自己的时候,也不会影响父辈的属性

不过还有一点点小问题,就是构造出来的Me.prototype上的 construtor 仍然是Father函数

function Father(){}
Father.prototype.name = 'han'
var father = new Father()

function inherit(Target, Origin){
  function F(){}
  F.prototype = Origin.prototype
  Target.prototype =  new F()
}

inherit(Me, Father)
function Me(){}
var me = new Me()
Me.prototype.age = 12
console.log(Father.prototype.constructor)  // Father(){}
console.log(Me.prototype.constructor)      // Father(){}

为了让Me.prototype 的constructor指向它自己的函数,需要再进行一次改造

function Father(){}
Father.prototype.name = 'han'
var father = new Father()

function inherit(Target, Origin){
  function F(){}
  F.prototype = Origin.prototype
  Target.prototype = new F()
  Target.prototype.constructor = Target      // 这是新添加的一行代码
  Target.prototype.uber = Origin.prototype   // 添加超类,就是继承最终的源头
}

inherit(Me, Father)
function Me(){}
var me = new Me()
Me.prototype.age = 12
console.log(Father.prototype.constructor)  // Father(){}
console.log(Me.prototype.constructor)      // Me(){}

有一个叫YUI的库是这样写inherit函数的

var inherit = function (Target, Origin){
  function F(){}
  return function(){
    F.prototype = Origin.prototype
    Target.prototype = new F()
    Target.prototype.constructor = Target
    Target.prototype.uber = Origin.prototype
  }
}

因为 function F 是一个中间变量,所以,这里使用一个闭包,将其私有化。但是功能是一样的

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

推荐阅读更多精彩内容