Skip to content

JavaScript深入之继承的多种方式和优缺点 #16

@mqyqingfeng

Description

@mqyqingfeng
Owner

写在前面

本文讲解JavaScript各种继承方式和优缺点。

但是注意:

这篇文章更像是笔记,哎,再让我感叹一句:《JavaScript高级程序设计》写得真是太好了!

1.原型链继承

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

Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

console.log(child1.getName()) // kevin

问题:

1.引用类型的属性被所有实例共享,举个例子:

function Parent () {
    this.names = ['kevin', 'daisy'];
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy", "yayu"]

2.在创建 Child 的实例时,不能向Parent传参

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

function Parent () {
    this.names = ['kevin', 'daisy'];
}

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

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy"]

优点:

1.避免了引用类型的属性被所有实例共享

2.可以在 Child 中向 Parent 传参

举个例子:

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

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

var child1 = new Child('kevin');

console.log(child1.name); // kevin

var child2 = new Child('daisy');

console.log(child2.name); // daisy

缺点:

方法都在构造函数中定义,每次创建实例都会创建一遍方法。

3.组合继承

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

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {

    Parent.call(this, name);
    
    this.age = age;

}

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

var child1 = new Child('kevin', '18');

child1.colors.push('black');

console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');

console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

优点:融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。

4.原型式继承

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

就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。

缺点:

包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。

var person = {
    name: 'kevin',
    friends: ['daisy', 'kelly']
}

var person1 = createObj(person);
var person2 = createObj(person);

person1.name = 'person1';
console.log(person2.name); // kevin

person1.friends.push('taylor');
console.log(person2.friends); // ["daisy", "kelly", "taylor"]

注意:修改person1.name的值,person2.name的值并未发生改变,并不是因为person1person2有独立的 name 值,而是因为person1.name = 'person1',给person1添加了 name 值,并非修改了原型上的 name 值。

5. 寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。

function createObj (o) {
    var clone = Object.create(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}

缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。

6. 寄生组合式继承

为了方便大家阅读,在这里重复一下组合继承的代码:

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {
    Parent.call(this, name);
    this.age = age;
}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

console.log(child1)

组合继承最大的缺点是会调用两次父构造函数。

一次是设置子类型实例的原型的时候:

Child.prototype = new Parent();

一次在创建子类型实例的时候:

var child1 = new Child('kevin', '18');

回想下 new 的模拟实现,其实在这句中,我们会执行:

Parent.call(this, name);

在这里,我们又会调用了一次 Parent 构造函数。

所以,在这个例子中,如果我们打印 child1 对象,我们会发现 Child.prototype 和 child1 都有一个属性为colors,属性值为['red', 'blue', 'green']

那么我们该如何精益求精,避免这一次重复调用呢?

如果我们不使用 Child.prototype = new Parent() ,而是间接的让 Child.prototype 访问到 Parent.prototype 呢?

看看如何实现:

function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {
    Parent.call(this, name);
    this.age = age;
}

// 关键的三步
var F = function () {};

F.prototype = Parent.prototype;

Child.prototype = new F();


var child1 = new Child('kevin', '18');

console.log(child1);

最后我们封装一下这个继承方法:

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

function prototype(child, parent) {
    var prototype = object(parent.prototype);
    prototype.constructor = child;
    child.prototype = prototype;
}

// 当我们使用的时候:
prototype(Child, Parent);

引用《JavaScript高级程序设计》中对寄生组合式继承的夸赞就是:

这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。

相关链接

《JavaScript深入之从原型到原型链》

《JavaScript深入之call和apply的模拟实现》

《JavaScript深入之new的模拟实现》

《JavaScript深入之创建对象》

深入系列

JavaScript深入系列目录地址:https://github.com/mqyqingfeng/Blog

JavaScript深入系列预计写十五篇左右,旨在帮大家捋顺JavaScript底层知识,重点讲解如原型、作用域、执行上下文、变量对象、this、闭包、按值传递、call、apply、bind、new、继承等难点概念。

如果有错误或者不严谨的地方,请务必给予指正,十分感谢。如果喜欢或者有所启发,欢迎star,对作者也是一种鼓励。

Activity

jawil

jawil commented on May 12, 2017

@jawil

博主还没写事件循环和浏览器渲染机制这块,借宝地这里有个问题提前探讨一下。

<html>
<body>
    <h2>Hello</h2>
    <script>
    function printH2() {
        console.log('first script', document.querySelectorAll('h2'));
    }
    printH2()
    setTimeout(printH2)
    </script>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/4.0.0-alpha.4/css/bootstrap.css">
    <h2>World</h2>
    <script>
    console.log('second script');
    </script>
</body>
</html>

这个demo的效果是:控制台先打印出来了printH2(),setTimeout(printH2)的结果(说明此时的DOMtree已经完成),然后浏览器显示了页面,页面完全显示之后(RenderTree完成)才执行了 console.log('second script');

有个地方不明白,就是js的异步任务与UI线程之间的调度,这个demo的结果来看,DOM节点渲染之后生成DOMtree才开始执行 setTimeout(printH2)的printH2函数,所以打印出了两个h2节点。
博主能帮忙解答一下UI线程和异步任务之间的关系到底是怎么样的吗?或者对于这一块有什么好的文章学习吗

mqyqingfeng

mqyqingfeng commented on May 12, 2017

@mqyqingfeng
OwnerAuthor

哈哈,这道题难住我了,对这个方面并没有研究,抱歉也提供不了什么资料,不过我会把这个课题记下来,如果想明白了或者有所进展,立刻跟题主讨论哈~

shaopower

shaopower commented on May 12, 2017

@shaopower

@jawil ,其实不一定是 setTimeout(printH2) 早于 second script 的
要看 bootstrap.css 这个文件获取的时机,如果 css 已经被浏览器缓存着的话(譬如第二次访问) second script 可能会先于 setTimeout。

照理说second script 应该是会被 css 阻塞执行的。不过之前都是看的 css 在 head 里的表现,放在 body 里是不是也有阻塞一说不太清楚。

只是我测下来的现象 disable cache 的话怎么刷新 second script 肯定在最后,但是把 disable cache 勾选去掉的话,后两个出现的顺序不固定。

浏览器里面的线程也希望有个大神能分享下,之前也遇到过很奇怪的表现,譬如 js 早于 css 或者不晚于 css 10ms 以内,浏览器会等 js 执行完再渲染;如果 晚于 css 10ms 之后网络再收到 js 响应,浏览器会先渲染再执行 js。

mqyqingfeng

mqyqingfeng commented on May 12, 2017

@mqyqingfeng
OwnerAuthor

@shaopower 竟然还有这么奇怪的表现,感觉开启了新的世界,看来要尽快的去探索一下~

jawil

jawil commented on May 12, 2017

@jawil
  • CSS(外链或内联)会阻塞整个DOM的渲染(Rendering),然而DOM解析(Parsing)会正常进行
  • 很多浏览器中,CSS会延迟脚本执行和DOMContentLoaded事件
  • JS(外链或内联)会阻塞后续DOM的解析(Parsing),后续DOM的渲染(Rendering)也将被阻塞
  • JS前的DOM可以正常解析(Parsing)和渲染(Rendering)

但是,我不明白的是,既然CSS会延迟脚本的执行,我把bootstrap那个换成内联样式,就是不发送http请求下载那个css样式,好像结果又变了,这时候构建CSStree好像并没有阻塞js脚本的执行,那么,到底是HTTP线程阻塞了脚本的执行,还是构建CSStree阻塞了脚本执行?

如果是HTTP线程阻塞了js的执行,这个也可以解释,为什么多个js并行下载,要等全部下载完成才执行一个道理,我继续尝试了一下,换成img标签,这个也会发送http请求,但是这个并不会阻塞脚本的执行。

如果是构建CSStree阻塞了线程,根据上面实践的结果,发现构建CSStree并没有阻塞脚本的运行。之前看到的下面的这个说法,也就不成立。

JS 的执行有可能依赖最新样式。 比如,可能会有 var width = $('#id').width(). 这意味着,JS 代码在执行前,浏览器必须保证在此 JS 之前的所有 css(无论外链还是内嵌)都已下载和解析完成。这是 CSS 阻塞后续 JS 执行的根本原因。

那么我的猜测就是,发送的请求是是样式文件会阻塞js脚本的执行,但为什么css会阻塞js脚本文件的执行,这个我暂时也不清楚,还有浏览器预加载一些机制也不太清楚。JS 的执行有可能依赖最新样式???难道我测的结果有问题?

jawil

jawil commented on May 12, 2017

@jawil

HTML5规范要求:
脚本执行前,出现在当前<script>之前的<link rel="stylesheet">必须完全载入。
脚本执行会阻塞DOM解析。

CSS外链阻塞了脚本的执行,此时异步队列的任务该如何调度呢?我想JS引擎主线程遇到阻塞后,这时候就会放弃当前线程的代码执行,这时候JS引擎是空闲的,为了避免等待白白浪费时间,所以主线程才会读取任务队列,开始执行异步任务。等CSS外链下载完成之后console.log('second script')难道成了下一个task

还有就是别人总结的这句话,一直不太懂,这种循坏机制到底是怎么工作的,描述的也比较抽象。

同时,异步任务又可以分为两种,macrotask(宏任务)和micro(微任务)。在挂起任务时,JS 引擎会将所有任务按照类别分到这两个队列中,首先在 macrotask 的队列(这个队列也被叫做 task queue)中取出第一个任务,执行完毕后取出 microtask 队列中的所有任务顺序执行;之后再取 macrotask 任务,周而复始,直至两个队列的任务都取完。

😨😨😨,浏览器渲染解析线程调度这一块任重而道远。

mqyqingfeng

mqyqingfeng commented on May 12, 2017

@mqyqingfeng
OwnerAuthor

嗯嗯,确实任重而道远,暂时我也没有想明白,还需要学习很多东西,非常感谢你的分析~ o( ̄▽ ̄)d

shaopower

shaopower commented on May 12, 2017

@shaopower

我觉得 js 脚本之前如果没有出现过 css 标签,那这部分脚本是不会被 css 文件阻塞的,就是说下面这块代码不会被阻塞

function printH2() {
    console.log('first script', document.querySelectorAll('h2'));
}
printH2()
setTimeout(printH2)

bootstrap.css 影响阻塞的是 second script,而且我觉得只有外链的 css 才会阻塞 js,内联的不会
在 chrome 的 timeline 或者 performance下可以看到

css 外链的时候

parse html;  
request bootstrap.css;
printH2();
setTimeout(printH2);
if (css 有缓存或者响应快){ 
    //receive css
    parse stylesheet;    
    second script;
    paint 页面;
    timeout fire;
} else {
    timeout fire;
    // receive css
    parse stylesheet;
    second script;
    paint 页面;
}

可以看到 second script 是在 parse stylesheet 之后的。

为什么有这个 if else 分支,我猜测浏览器做的某些处理,只是保证不会解析 css 后的 js 加入 event loop,也就不会执行了, 但是已经在里面的还是会执行完吧

css 改成内联后

走的逻辑类似 if css 缓存那个分支,而且没有 parse stylesheet 这一条了,parse html 也有差别,最开始就直接 parse 完了整个页面

具体为什么 css 会延迟 js 执行,我觉得这是浏览器的一种实现方式吧。

如果浏览器尚未完成 CSSOM 的下载和构建,而我们却想在此时运行脚本,会怎样?答案很简单,对性能不利:浏览器将延迟脚本执行和 DOM 构建,直至其完成 CSSOM 的下载和构建。

https://developers.google.com/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript

另外并行的 js 请求也不是等全部都下载完才执行的哈,如果 script 不带 async 的话,按照在 html 里的上下位置来执行的,当然和网络也有关系。

例如下面的 2个 js 会同时请求
如果 1.js 5秒后返回,2.js 10秒后返回。在5秒的时候 1.js 会执行,10秒的时候 2.js 执行。
但是如果反过来 1.js 10秒后返回,2.js 5秒,那会在10秒的时候执行 1.js,1.js执行完开始再执行 2.js

<body>
<script src="1.js"></script>
<script src="2.js"></script>
</body>

setTimeout 是属于 macrotask,你的例子里应该和 microtask 关系不大, Promise then 和 MutationObserver 这些算 microtask

littleluckly

littleluckly commented on May 12, 2017

@littleluckly

写的很透彻,我得慢慢消化

littleluckly

littleluckly commented on May 13, 2017

@littleluckly

请教一个问题。为什么不让 Child.prototype 直接访问到 Parent.prototype ?一定要通过寄生的方式呢。。。
`
function Parent (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
console.log(this.name)
}

function Child (name, age) {
Parent.call(this, name);
this.age = age;
}

// 关键的三步
//var F = function () {};

//F.prototype = Parent.prototype;

//Child.prototype = new F();
//我的修改
Child.prototype = Parent.prototype;

var child1 = new Child('kevin', '18');

console.log(child1);`

shaopower

shaopower commented on May 13, 2017

@shaopower

@littleluckly
你这样的话,增加 Child.prototype.testProp = 1; 同时会影响 Parent.prototype 的
如果不模拟,直接上 es5 的话应该是下面这样吧
Child.prototype = Object.create(Parent.prototype);

mqyqingfeng

mqyqingfeng commented on May 13, 2017

@mqyqingfeng
OwnerAuthor

@shaopower 十分感谢回答,正解,b( ̄▽ ̄)d

littleluckly

littleluckly commented on May 13, 2017

@littleluckly

@shaopower @mqyqingfeng 谢谢解惑!

153 remaining items

lcxhxf

lcxhxf commented on Oct 5, 2022

@lcxhxf

第一个例子,原型链继承console.log(child1.getName()) // kevin是否换成child1.getName()就可以了🤣🤣🤣

cliYao

cliYao commented on Oct 5, 2022

@cliYao
qinran0423

qinran0423 commented on Feb 11, 2023

@qinran0423

大佬 我这有个小疑问

function Parent() {
  this.age = 18;
}

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

child1.age = 19;
console.log(child1.age);

原型链继承 这种修改age 为什么是在实例上新加了个属性 而不是修改原型的值

66njk

66njk commented on Feb 11, 2023

@66njk

大佬 我这有个小疑问

function Parent() {
  this.age = 18;
}

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

child1.age = 19;
console.log(child1.age);

原型链继承 这种修改age 为什么是在实例上新加了个属性 而不是修改原型的值

https://blog.csdn.net/helloschurmi/article/details/95043685 这个博客解释的比较清楚

qinran0423

qinran0423 commented on Feb 11, 2023

@qinran0423

大佬 我这有个小疑问

function Parent() {
  this.age = 18;
}

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

child1.age = 19;
console.log(child1.age);

原型链继承 这种修改age 为什么是在实例上新加了个属性 而不是修改原型的值

https://blog.csdn.net/helloschurmi/article/details/95043685 这个博客解释的比较清楚

多谢

gdmec07150725

gdmec07150725 commented on Feb 15, 2023

@gdmec07150725

为什么会用寄生这个词的?

alexgy1

alexgy1 commented on Jul 6, 2023

@alexgy1

大佬 我这有个小疑问

function Parent() {
  this.age = 18;
}

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

child1.age = 19;
console.log(child1.age);

原型链继承 这种修改age 为什么是在实例上新加了个属性 而不是修改原型的值

可以从js内存模型的角度理解, 基本类型存在stack里面,引用类型存在堆内存里面,
js里面函数也是对象,基于这一个前提,就可以得知, child1 对象在堆内存里面开辟一个地址A,
Child.prototype = new Parent(); 这句话说明 Child的原型是一个新的对象,得到child原型在堆内存里面开辟一个地址B

你的问题 就很容易得到答案了, 为什么改了A.xx 不会影响B . 因为指向的不是一个地址。

cliYao

cliYao commented on Jul 6, 2023

@cliYao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @yuankui@gnosis23@zhidong10@aaronlam@beyond5959

        Issue actions

          JavaScript深入之继承的多种方式和优缺点 · Issue #16 · mqyqingfeng/Blog