js中的继承

上篇说了js的设计模式,现在再说说继承(不理解设计模式的可以看上篇)。
1、原型链

function Child(){
    this.name= 'scater';
}
function Parent(){
    this.age= 29;
}
//继承了 SuperType
Child.prototype = new Parent();
Parent.prototype.getAge= function (){
    return this.age;
};
const child1 = new Child();
console.log(child1.getAge()); //29

缺点:引用类型属性被实例共享;创建子类实例时,无法向父类构造函数传参。
2、借用构造函数

function Child(){
    this.name= 'scater';
    Parent.call(this); // call和apply都可以
}
function Parent(){
    this.age= 29;
    this.getAge= function (){
        return this.age;
    }
};
const child1 = new Child();
console.log(child1.getAge()); //29

实例并不是父类的实例,只是子类的实例;只能继承父类的实例属性和方法,不能继承原型属性/方法;无法实现函数复用,每个子类都有父类实例函数的副本,影响性能
3、组合继承

function Parent(){
    this.age= 29;
    this.getAge= function (){
        return this.age;
    }
};
function Child(){
    this.name= 'scater';
    Parent.call(this); // call和apply都可以
}
Child.prototype = new Parent();
Child.prototype.constructor = Child; // 将构造函数指向Child
Parent.prototype.getName = function (){
    return this.name;
};
const child1 = new Child();
console.log(child1.getAge(), child1.getName ()); //29, 'scater'

可以继承父级实例属性/方法,也可以继承父级原型属性/方法;但是调用了两次父类构造函数,生成了两份实例。
4、原型式继承

function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}
var person = {
    name: "scater",
    friends: ["tom", "Lee"]
};
// 利用object继承
const anotherPerson = object(person);
// 给子类添加自有属性
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
console.log (anotherPerson.name, anotherPerson.friends);  // Greg ["tom", "Lee", "Rob"]

ES5 通过新增 Object.create() 方法规范化了原型式继承。这个方法接收两个参数:一
个用作新对象原型的对象和(可选的)一个为新对象定义额外属性的对象。

var anotherPerson = Object.create(person);
// 给子类添加自有属性
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
console.log (anotherPerson.name, anotherPerson.friends);  // Greg ["tom", "Lee", "Rob"]

在没有必要创建构造函数,而只想让一个对象与另一个对象保持类似的情况下,原型式
继承是完全可以胜任的。但是包含引用类型值的属性始终都会共享相应的值,就像使用原型模
式一样。
5、寄生式继承

function object(o){
    function F(){}
    F.prototype = o;
    return new F();
}
function createAnother(original){
    var clone = object(original); //通过调用函数创建一个新对象
    clone.sayHi = function(){ //以某种方式来增强这个对象
        console.log("hi");
    };
    return clone; //返回这个对象
}
var person = {
    name: "Nicholas",
    friends: ["Shelby", "Court", "Van"]
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi(); //"hi"

在主要考虑对象而不是自定义类型和构造函数的情况下,寄生式继承也是一种有用的模式。
6、寄生组合式继承(推荐)

function Parent(){
    this.age= 29;
}
function Child(name){
  Parent.call(this);
  this.name = name;
}
(function(){
  // 创建一个没有实例方法的类
  var Super = function(){};
  Super.prototype = Parent.prototype;
  //将实例作为子类的原型
  Child.prototype = new Super();
})();

Child.prototype.constructor = Child; // 将构造函数指向Child
const scater= new Child('scater');
console.log(scater.name, scater.age); // scater 29

7、ES6 Class类的继承

class Parent {
  constructor() {
    this.age = 29;
  }
}
class Child extends Parent {
  constructor(name) {
    super(); // 在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。
    this.name = name;
  }
}
const scater = new Child('scater');
console.log(scater.name, scater.age); // scater 29

ES6 的继承机制,实质是先将父类实例对象的属性和方法,加到this上面(所以必须先调用super方法),然后再用子类的构造函数修改this。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 方式一、原型链继承 分析: 这种方式实现的本质是通过将子类的原型指向了父类的实例,子类的实例就可以通过proto访...
    tency小七阅读 1,742评论 0 0
  •   面向对象(Object-Oriented,OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意...
    霜天晓阅读 6,411评论 0 6
  • # 概述 本文主要记录js中实现继承的几种方法的实现,优缺点分析 # 知识铺垫 + **构造函数,原型和实例的关系...
    HLE阅读 3,795评论 0 0
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 7,881评论 2 9
  • 第3章 基本概念 3.1 语法 3.2 关键字和保留字 3.3 变量 3.4 数据类型 5种简单数据类型:Unde...
    RickCole阅读 10,620评论 0 21