众所周知,在ES6
以前,前端是不存在类的语法糖,因此不能像其余语言同样用extends
关键字就搞定继承关系,须要一些额外的方法来实现继承。下面就介绍一些经常使用的方法,红宝书已经归纳的十分全面了,因此本文基本就是对红宝书继承篇章的笔记和梳理。前端
function Parent() {
this.name = 'arzh'
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child() {
}
//主要精髓所在
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild = new Child()
arzhChild.getName() // 'arzh'
复制代码
原型链继承缺点:bash
function Parent() {
this.names = ['arzh','arzh1'];
}
function Child() {
}
//主要精髓所在
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild2 = new Child()
arzhChild2.names.push('arzh2')
console.log(arzhChild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhChild3 = new Child()
arzhChild3.names.push('arzh3')
console.log(arzhChild3.names) //[ 'arzh', 'arzh1', 'arzh2', 'arzh3' ]
复制代码
Child
实例的时候,没法向Parent
传参。这样就会使Child
实例无法自定义本身的属性(名字)function Parent() {
this.names = ['arzh','arzh1']
}
function Child() {
Parent.call(this)
}
var arzhChild2 = new Child()
arzhChild2.names.push('arzh2')
console.log(arzhChild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhChild3 = new Child()
arzhChild3.names.push('arzh3')
console.log(arzhChild3.names) //[ 'arzh', 'arzh1', 'arzh3' ]
复制代码
优势:函数
function Parent(name) {
this.name = name
}
function Child(name) {
Parent.call(this, name)
}
var arzhChild = new Child('arzh');
console.log(arzhChild.name); // arzh
var arzhChild1 = new Child('arzh1');
console.log(arzhChild1.name); // arzh1
复制代码
缺点:优化
function Parent(name) {
this.name = name
this.body = ['foot','hand']
}
function Child(name, age) {
Parent.call(this, name)
this.age = age
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild1 = new Child('arzh1', '18')
arzhChild1.body.push('head1')
console.log(arzhChild1.name,arzhChild1.age) //arzh1 18
console.log(arzhChild1.body) //[ 'foot', 'hand', 'head1' ]
var arzhChild2 = new Child('arzh2', '20')
arzhChild2.body.push('head2')
console.log(arzhChild2.name,arzhChild2.age) //arzh2 20
console.log(arzhChild2.body) //[ 'foot', 'hand', 'head2' ]
复制代码
优势:ui
缺点:this
Child.prototype = new Parent()
,第二次是Parent.call(this, name)
形成没必要要的浪费复制传入的对象到建立对象的原型上,从而实现继承spa
function createObj(o) {
function F(){}
F.prototype = o;
return new F();
}
var person = {
name : 'arzh',
body : ['foot','hand']
}
var person1 = createObj(person)
var person2 = createObj(person)
console.log(person1) //arzh
person1.body.push('head')
console.log(person2) //[ 'foot', 'hand', 'head' ]
复制代码
缺点: 同原型链继承同样,每一个实例对引用类型属性的修改都会被其余的实例共享prototype
咱们可使用Object.create
来代替上述createObj
的实现,原理基本上是同样的。寄生式继承其实就是在createObj
的内部以某种形式来加强对象(这里的加强能够理解为添加对象的方法),最后返回加强以后的对象。code
function createEnhanceObj(o) {
//代替原型式继承的createObj
var clone = Object.create(o)
clone.getName = function () {
console.log('arzh')
}
return clone;
}
复制代码
经过createEnhanceObj
就能够在建立对象的时候,把对象方法也经过此种方式继承。
缺点: 同借用构造函数同样,没法复用父类函数,每次建立对象都会建立一遍方法cdn
不须要为了子类的原型而多new
了一次父类的构造函数,如Child.prototype = new Parent()
只须要复制父类原型的一个副本给子类原型便可
function inheritPrototype(Parent, Child){
Child.prototype = Object.create(Parent.prototype) //建立父类原型的一个副本,把副本赋值给子类原型
Child.prototype.constructor = Child;
}
function Parent(name) {
this.name = name
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child(color) {
Parent.call(this, 'arzh')
this.color = color
}
inheritPrototype(Parent, Child)
var arzhChild = new Child('red')
console.log(arzhChild.name) // 'arzh'
复制代码
优势: 没必要为了指定子类型的原型而调用父类型的构造函数
ES6
支持经过类来实现继承,方法比较简单,代码以下
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
toString() {
return this.x + '' + this.y
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y) //调用父类的constructor(x, y)
this.color = color
}
toString() {
return this.color + ' ' + super.toString() // 调用父类的toString()
}
}
var colorPoint = new ColorPoint('1', '2', 'red')
console.log(colorPoint.toString()) // red 12
复制代码
对本文有任何优化建议,可扫描下述二维码一块儿讨论,同时也但愿你们多多关注,会不按期发送一些原创文章