惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

静水深流's blog

探索 SSE:服务器推送技术的魅力与应用 | 静水深流 图解DIFF算法介绍 | 静水深流 如何使用javascript实现复制出的文案带链接? | 静水深流 基于vuepress2搭建专属自己的博客,并集成各种常用功能 | 静水深流 听说你至今不晓得缓存淘汰算法?实现LRU、LFU和FIFO? | 静水深流 最长递增子序列及vue3.0中diff算法 | 静水深流 二进制之入门到应用实践 | 静水深流 常见算法学习 | 静水深流 CSS 形状的实现 | 静水深流 ajax取消接口请求 | 静水深流 前端常见的安全问题 | 静水深流 关于http服务端的学习&总结 | 静水深流 前端面试题总结 | 静水深流 javascript原生代码实现及代码总结 | 静水深流 LeetCode算法学习总结-简单 | 静水深流 LeetCode算法学习总结-困难 | 静水深流 LeetCode算法学习总结- 中等 | 静水深流 排序算法总结 | 静水深流 扫码登录的实现原理 | 静水深流 Javascript之常见类型判断汇总 | 静水深流 webpack开发、使用及优化总结 | 静水深流 从JavaScript中的拷贝开始思考 | 静水深流 vue原理、使用及面试方面的总结 | 静水深流 前端发展及选择 | 静水深流 css面试总结 | 静水深流 JavaScript 数组展开(扁平化)和underscore的 flatten | 静水深流 文章列表 | 静水深流 首页 | 静水深流 学习网站收藏 | 静水深流
JavaScript各种继承方式和优缺点 | 静水深流
2021-03-12 · via 静水深流's blog

原型链继承

function SuperClass() {  
    this.superValue = true;  
  }  
  //为父类添加公有方法  
  SuperClass.prototype.getSuperValue = function () {  
    return this.superValue;  
  };  
  //声明子类  
  function SubClass() {  
    this.subValue = false;  
  }  
  //继承父类  
  SubClass.prototype = new  SuperClass();//将父类对象赋值给子类原型,子类原型可访问父类原型上的属性和方法--类式继承原理  
  //子类添加公有方法  
  SubClass.prototype.getSubValue = function() {  
    return this.subValue;  
  }; 

缺点:
1、子类通过其原型prototype对父类实例化,继承了父类。但当父类中的共有子类通过其原型prototype对父类实例化,继承了父类。但当父类中的共有属性是引用类型时,会在子类中被所有的实例共用,如此在一个子类实例中更改从父类中继承过来的公有属性时,会影响到其他子类
2、由于子类是通过原型prototype实例化父类实现继承的,所以在创建子类的时候,无法向父类传递参数,

借用构造函数(经典继承)

function SuperClass(id) {  
    this.book = ['javascript','html','css'];//引用类型共有属性  
    this.id = id;//值类型公有属性  
  }  
  //父类声明原型方法  
  SuperClass.prototype.showBooks = function() {  
    console.log(this.books);  
  }  
  //声明子类  
  function SubClass(id) {  
    //继承父类  
    SuperClass.call(this,id);  
  } 

优点:
1、避免了引用类型的属性被所有实例共享
2、可以向父类传参 缺点:
1、父类的原型方法不会被子类继承 2、方法都在构造函数中定义,每次创建实例都会创建一遍方法。

组合式继承

原型链继承和经典继承双剑合璧

function SuperClass(name) {  
    this.name = name;  
    this.book = ['javascript','html','css'];  
}  
SuperClass.prototype.getName = function () {  
    console.log(this.name);  
};  
function SubClass(name,time) {  
    //构造函数式继承,继承父类name属性  
    SuperClass.call(this,name);  
    this.time = time;  
}  
  //类式继承,子类原型继承  
SubClass.prototype = new SuperClass();  
  //子类原型方法  
SubClass.prototype.getTime = function () {  
    console.log(this.time);  
};

优点:
融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式
缺点:
父类的构造函数执行了两遍:一次在子类的构造函数中call方法执行一遍,一次在子类原型实例化父类的时候执行一遍。

原型式继承

类似于Object.create的模拟实现,将传入的值当作创建对象的原型

function createObj(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

ECMAScript5通过新增 Object.create()方法规范了原型式继承 缺点:包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。

寄生组合式继承

借用构造函数 + 原型式继承的结合体;是一种比较常见的成熟的方式

// 原型继承
function inheritPrototype(subType, superType){
  var prototype = Object.create(superType.prototype); // 创建对象
  prototype.constructor = subType;                    // 增强对象
  subType.prototype = prototype;                      // 指定对象
}

// 父类初始化实例属性和原型属性
function SuperClass(name){
  this.name = name;  
   this.book = ['javascript','html','css'];  
}
SuperClass.prototype.getName = function(){
  alert(this.name);
};

// 借用构造函数继承
function SubClass(name, age){
  SuperClass.call(this, name);
  this.time = time;
}

// 将父类原型指向子类
inheritPrototype(SubClass, SuperClass);

// 新增子类原型属性
SubClass.prototype.getTime = function(){
  alert(this.time);
}

var instance1 = new SubClass("js",12);
var instance2 = new SubClass("scss", 12);

es6新增的extends方式

Class 可以通过extends关键字实现继承;他的继承核心和寄生组合式继承很像

class SuperClass {
}

class SubClass extends SuperClass {
  constructor(x, y) {
    super(x, y); // 调用父类的constructor(x, y)
    this.color = color;
  }
}