JS继承

继承

1. 原型链

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

2. 构造函数

Father.call(Child);

3. 组合

Father.call(Child);
Child.prototype = new Father();
Child.prototype.constructor = Child;

4. 探索

Father.call(Child);
Child.prototype = Father.prototype;
Child.prototype.constructor = Child;

5. 成功

  • 使用 Object.create
Father.call(Child);
Child.prototype = Object.create(Father.prototype)//以Father.prototype对象为原型,生成了Child.prototype对象
Child.prototype.constructor = Child;
  • 不使用
(function () {
    // 创建一个没有实例方法的类
    var Super = function () { };
    Super.prototype = Father.prototype;
    //将实例作为子类的原型
    Child.prototype = new Super();
    Child.prototype.constructor = Child;
})();

6. 升级Class

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

推荐阅读更多精彩内容