1. 原型链继承 (原型链)

function Parent() {
this.fruits = ['apple', 'orange'];
}
Parent.prototype.sayHello = function () {
console.log('Parent');
}; function Child() {} Child.prototype = new Parent(); let child1 = new Child();
child1.fruits.push('banana');
console.log(child1.fruits); // [ 'apple', 'orange', 'banana' ] let child2 = new Child();
console.log(child2.fruits); // [ 'apple', 'orange', 'banana' ]

特点:被所有的实例共享了引用类型属性


2. 构造函数方式的继承 (call)

function Parent() {
this.fruits = ['apple', 'orange'];
}
Parent.prototype.sayHello = function () {
console.log('Parent');
}; function Child() {
Parent.call(this);
} let child1 = new Child();
child1.fruits.push('banana');
console.log(child1.fruits); // [ 'apple', 'orange', 'banana' ] let child2 = new Child();
console.log(child2.fruits); // [ 'apple', 'orange' ]

特点:

  1. call方法仅仅调用了父级构造函数的属性及方法(私有属性),没有办法调用父级构造函数原型对象的方法(公有属性)
  2. 可以在 Child 中向 Parent 传参
  3. 每次实例化对象都得创建一遍方法,基本无法实现函数复用

3. 组合链继承方式 (原型链 + call)

function Parent(name) {
this.name = name;
this.fruits = ['apple', 'orange'];
}
Parent.prototype.sayHello = function () {
console.log(this.name);
}; function Child(name, age) {
Parent.call(this, name);
this.name = age;
}
Child.prototype = new Parent(); let child1 = new Child();
child1.fruits.push('banana');
console.log(child1.fruits); // [ 'apple', 'orange', 'banana' ] let child2 = new Child();
console.log(child2.fruits); // [ 'apple', 'orange' ]

特点:

  1. 子类的私有属性继承了父类私有属性,子类公有属性也继承了父类的私有属性和公有属性
  2. 父类函数执行了两次

4. 寄生继承 (Object.create 或者是自我实现的Object)

let parent = {
fruits: ['apple', 'orange'],
sayHello: function () {
console.log('hello');
},
}; function parasitic(pro) {
let clone = Object.create(pro);
return clone;
} let child1 = parasitic(parent);
child1.fruits.push('banana');
console.log(child1.fruits); // [ 'apple', 'orange', 'banana' ] let child2 = parasitic(parent);

这里用到了Object.create

特点:

  1. 和原型链继承效果雷同

5. 寄生组合继承(Oject.create + call)

function Parent(name) {
this.name = name;
this.fruits = ['apple','orange'] }
Parent.prototype.sayHello = function () {
console.log(this.name)
} function Child() {
Parent.call(this) }
function parasitic(child,parent) {
let tempObj = Object.create(parent.prototype);
child.prototype = tempObj;
tempObj.constructor = child;
} parasitic(Child, Parent);

特点:

  1. 子类私有属性继承了父类的私有属性,子类公有属性继承了父类公有属性(和组合继承相比更加纯粹干净)

总结: 所以应用的最多应该就是 组合继承和寄生组合继承两种方式了

js继承方式及特征的更多相关文章

  1. JS继承方式详解

    js继承的概念 js里常用的如下两种继承方式: 原型链继承(对象间的继承) 类式继承(构造函数间的继承) 由于js不像java那样是真正面向对象的语言,js是基于对象的,它没有类的概念.所以,要想实现 ...

  2. 经典面试题:js继承方式下

    上一篇讲解了构造函数的继承方式,今天来讲非构造函数的继承模式. 一.object()方法 json格式的发明人Douglas Crockford,提出了一个object()函数,可以做到这一点. fu ...

  3. 经典面试题:js继承方式上

    js不是传统的面向对象语言,那么他是怎么实现继承的呢?由于js是基于原型链实现的面向对象,所以js主要通过原型链查找来实现继承,主要有两大类实现方式,分为基于构造函数的继承,以及非构造函数的继承. 由 ...

  4. js继承方式

    1.原型链 实现的本质是重写原型对象,代之以一个新类型的实例: 给原型添加方法的代码硬顶放在替换原型的语句之后: 不能使用对象字面量查收能见原型方法,这样会重写原型链. 缺点:包含引用类型值的原型属性 ...

  5. js继承方式及其优缺点?

    原型链继承的缺点一是字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数.借用构造函数(类式继承)借用构造函数虽然解决了刚才两种问题,但没有原型,则复用无从谈起.所以我们需要 ...

  6. js 中继承方式小谈

    题外话 前段时间面试中笔试题有这道题目: 请实现一个继承链,要求如下: 构造函数A():构造函数中有consoleA方法,可以实现console.log("a") 实例对象 a:a ...

  7. js的三种继承方式及其优缺点

    [转] 第一种,prototype的方式: //父类 function person(){ this.hair = 'black'; this.eye = 'black'; this.skin = ' ...

  8. 重新理解JS的6种继承方式

    写在前面 一直不喜欢JS的OOP,在学习阶段好像也用不到,总觉得JS的OOP不伦不类的,可能是因为先接触了Java,所以对JS的OO部分有些抵触. 偏见归偏见,既然面试官问到了JS的OOP,那么说明这 ...

  9. js两种定义函数、继承方式及区别

    一:js两种定义函数的方式及区别 1:函数声明: function sayA() { alert("i am A"); } 2:函数表达式: var sayB = function ...

随机推荐

  1. 11 监控MySQL主从状态是否异常

    #!/bin/bash source /etc/profile # 主从同步 # master:binlog # slave:relaylog # 写->master->binlog< ...

  2. 别错过了,130+个微信小程序源码 “限时分享“

    ​里面有130+款微信小程序源码和效果图,我只放了其中几款小程序的截图,具体请看下方图片 ​ ​ ​ ​ ​ ​ ​ ​ 仿网易云音乐小程序源码 链接:https://pan.baidu.com/s/ ...

  3. Html:id,name,class之间的有什么区别?

    name 一个name可以同时对应多个控件,比如checkbox和radio,主要用于获取提交表单的某表单域信息, 作为可与服务器交互数据的HTML元素的服务器端的标示,比如input.select. ...

  4. kafka错误集锦

    javax.management.InstanceAlreadyExistsException: kafka.consumer:type=FetchRequestAndResponseMetrics, ...

  5. redis-cluster集群安装(windows)

    在此先奉上安装包(链接:https://pan.baidu.com/s/1QHYQPkYPuiRWhdj9APbjnw 提取码:jv8x ) 1. 安装ruby 下载 rubyinstaller-2. ...

  6. python 正则表达式 中级

    1.子表达式 将几个字符的组合形式看做一个大的字符,例如匹配IP地址,形如 127.0.0.1 答案一:p1='\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' pattern1 ...

  7. charles证书导入系统信任区(Android7.0以上)

    打开charles,跟着下图来,下载好charles的证书 后缀是pem的格式,挺方便的了,burp的证书是der的,还需要再进一步转化成pem,这里就不再多说, 利用openssl来计算出文件名 加 ...

  8. python 字符串 增、删、改、查基本操作

    private static String TAG = "MainActivity"; private String str = " a,bB,1cCcc,2dDd d2 ...

  9. Sharding+SpringBoot+Mybatis 读写分离

    基于Sharding JDBC的读写分离 1.引入pom.xml <dependencies> <!-- mybatis --> <dependency> < ...

  10. ssh服务两句话

    ssh服务采用"非对称密钥系统":主要通过两把不一样的公钥和密钥来进行加密与解密的过程 公钥(Public Key):提供给远程主机进行数据加密 私钥(Private Key):远 ...