美文网首页
浅析Javascript类的继承

浅析Javascript类的继承

作者: Yangfan2016 | 来源:发表于2018-10-30 21:21 被阅读6次

继承

构造函数继承

function Parent() {
    this.name='parent';
}

Parent.prototype.run=function () {
    console.log('We can run');
}

function Child() {
    Parent.call(this);
}

let c=new Child();
c.name; // 'parent'
c.run(); // undefined
  • 缺点
  1. 无法继承父类原型上的方法

原型链实现继承

function Parent() {
    this.name='parent';
    this.colors=["red","yellow"];
}

Parent.prototype.run=function () {
    console.log('We can run');
}

function Child() {
    
}

Child.prototype=new Parent();
Child.prototype.constructor=Child;   

var c1=new Child();
var c2=new Child();

c1.colors; // ['red','yellow']
c2.colors; // ['red','yellow']

c1.colors.push('green');

c1.colors; // ['red','yellow','green']
c2.colors; // ['red','yellow','green']
  • 缺点
  1. 子类共用一个父类的实例,造成数据污染
  2. 创建子类实例时,无法向父类构造函数传参

组合式继承

function Parent() {
    this.name='parent';
    this.colors=["red","yellow"];
    console.log("I've executed");
}

Parent.prototype.run=function () {
    console.log('We can run');
}

function Child() {
    Parent.call(this);
}

Child.prototype=new Parent();
Child.prototype.constructor=Child;

var c1=new Child(); // "I've executed" "I've executed"
var c2=new Child(); // "I've executed" "I've executed"

c1.colors; // ['red,yellow']
c2.colors; // ['red,yellow']

c1.colors.push('green');

c1.colors; // ['red,yellow','green']
c2.colors; // ['red,yellow']
  • 缺点
  1. 每新建一个子类都会调用两次父类构造器

寄生组合式继承


function Parent() {
    this.name='parent';
    this.colors=["red","yellow"];
}

Parent.prototype.run=function () {
    console.log('We can run');
}


function Child() {
    Parent.call(this);
}
function Super() {/* noop */}
Super.prototype=Parent.prototype;
Child.prototype=new Super();
Child.prototype.constructor=Child;

  • 特点
  1. 完美实现继承
  2. 利用寄生函数实现父类构造函数只执行一次,将原型上的方法挂载到寄生函数上,最后将寄生函数的实例作为子类的原型对象

ES6 继承


class Parent {
    constructor() {
        this.name='parent';
        this.colors=["red","yellow"];
    }
    run() {
        console.log('We can run');
    }
}

class Child extends Parent{
    constructor() {
        super();
        this.name='child';
    }
}

  • 特点
  1. 简单明了
  2. 只是一种语法糖而已
  • 下面是babel将ES6转成ES5继承的实现

Babel 编译后的ES5代码

"use strict";

// 创建类
var _createClass = function() {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value"in descriptor)
                descriptor.writable = true;
            Object.defineProperty(target, descriptor.key, descriptor);
        }
    }
    return function(Constructor, protoProps, staticProps) {
        // 定义原型对象的属性、方法
        if (protoProps)
            defineProperties(Constructor.prototype, protoProps);
        // 定义类的静态属性、方法
        if (staticProps)
            defineProperties(Constructor, staticProps);
        return Constructor;
    }
    ;
}();
// 确保子类初始化时调用super
function _possibleConstructorReturn(self, call) {
    if (!self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }
    return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
// 继承
function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
        throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
    }
    // 利用 Object.crete 创建了一个父类原型对象的新实例,并且修正了构造器指向
    subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
            value: subClass,
            enumerable: false,
            writable: true,
            configurable: true
        }
    });
    // 将子类原型链链到父类上
    if (superClass)
        Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
// 安全检测,确保用 'new' 操作的构造函数
function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

var Parent = function() {
    function Parent() {
        _classCallCheck(this, Parent);

        this.name = 'parent';
        this.colors = ["red", "yellow"];
    }

    _createClass(Parent, [{
        key: "run",
        value: function run() {
            console.log('We can run');
        }
    }]);

    return Parent;
}();

var Child = function(_Parent) {
    _inherits(Child, _Parent);

    function Child() {
        _classCallCheck(this, Child);

        var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this));

        _this.name = 'child';
        return _this;
    }

    return Child;
}(Parent);


相关文章

  • 浅析Javascript类的继承

    继承 构造函数继承 缺点 无法继承父类原型上的方法 原型链实现继承 缺点 子类共用一个父类的实例,造成数据污染 创...

  • 源码浅析 ArrayList、Vector、LinkedList

    从类的定义浅析 首先从继承父类来看: ArrayList & Vector 都继承了AbstractList 抽象...

  • JavaScript - 继承和类

    JavaScript - 继承和类 在这一篇中,我要聊聊 JavaScript 中的继承和“类”。 首先跟你请教下...

  • 浅析JavaScript继承(转)

    转自 https://my.oschina.net/qiangdada/blog/745061 继承算是JavaS...

  • javascript 类继承

    1.面向过程与面向对象 1.1面向过程 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,...

  • JavaScript浅析 -- 对象的继承

    一、继承 继承,是指一个对象(子对象)通过某种方法,使得自己可以访问另一个对象(父对象)的属性和方法。而js没有提...

  • JavaScript类的继承

    Object.create() 实现单继承 Object.assign() 实现多继承 Object.assign...

  • 5. oop继承

    JavaScript 继承实现方式 A. 类式继承 所谓类式继承,就是将子类的原型指向父类的一个实例。这样优缺点就...

  • 继承

    继承:让子类拥有父类的属性和方法//继承是类与类之间的关系谈继承是一个悖论:JavaScript是函数编程流派,是...

  • JS 继承

    1.原型链继承:prototype 2.原型链继承2:prototype 注意:JavaScript的类继承其实本...

网友评论

      本文标题:浅析Javascript类的继承

      本文链接:https://www.haomeiwen.com/subject/sguntqtx.html