面试官问:JS的继承

原文作者若川,掘金链接:https://juejin.im/post/5c433e216fb9a049c15f841b

写于2019年2月20日,现在发到公众号声明原创,之前被《前端大全》公众号等转载阅读量超1w+,知乎掘金等累计阅读量超过1w+。

导读:文章主要通过ES6的extends,结合ES5的寄生组合继承,图文并茂的讲述JS的继承,最后还推荐了一些书籍的继承的章节,旨在让读者掌握JS的继承。

用过 React的读者知道,经常用 extends继承 React.Component

// 部分源码
function Component(props, context, updater) {// ...
}
Component.prototype.setState = function(partialState, callback){// ...
}
const React = {Component,// ...
}
// 使用
class index extends React.Component{// ...
}

点击这里查看 React github源码

面试官可以顺着这个问 JS继承的相关问题,比如: ES6的 class继承用ES5如何实现。据说很多人答得不好。

构造函数、原型对象和实例之间的关系

要弄懂extends继承之前,先来复习一下构造函数、原型对象和实例之间的关系。代码表示:

function F(){}
var f = new F();
// 构造器
F.prototype.constructor === F; // true
F.__proto__ === Function.prototype; // true
Function.prototype.__proto__ === Object.prototype; // true
Object.prototype.__proto__ === null; // true
// 实例
f.__proto__ === F.prototype; // true
F.prototype.__proto__ === Object.prototype; // true
Object.prototype.__proto__ === null; // true

笔者画了一张图表示: 

ES6extends 继承做了什么操作

我们先看看这段包含静态方法的 ES6继承代码:

// ES6
class Parent{constructor(name){this.name = name;}static sayHello(){console.log('hello');}sayName(){console.log('my name is ' + this.name);return this.name;}
}
class Child extends Parent{constructor(name, age){super(name);this.age = age;}sayAge(){console.log('my age is ' + this.age);return this.age;}
}
let parent = new Parent('Parent');
let child = new Child('Child', 18);
console.log('parent: ', parent); // parent:  Parent {name: "Parent"}
Parent.sayHello(); // hello
parent.sayName(); // my name is Parent
console.log('child: ', child); // child:  Child {name: "Child", age: 18}
Child.sayHello(); // hello
child.sayName(); // my name is Child
child.sayAge(); // my age is 18

其中这段代码里有两条原型链,不信看具体代码。

// 1、构造器原型链
Child.__proto__ === Parent; // true
Parent.__proto__ === Function.prototype; // true
Function.prototype.__proto__ === Object.prototype; // true
Object.prototype.__proto__ === null; // true
// 2、实例原型链
child.__proto__ === Child.prototype; // true
Child.prototype.__proto__ === Parent.prototype; // true
Parent.prototype.__proto__ === Object.prototype; // true
Object.prototype.__proto__ === null; // true

一图胜千言,笔者也画了一张图表示,如图所示:

 结合代码和图可以知道。 ES6extends 继承,主要就是:

  • 1.把子类构造函数( Child)的原型( __proto__)指向了父类构造函数( Parent),

    2.把子类实例 child的原型对象( Child.prototype) 的原型( __proto__)指向了父类 parent的原型对象( Parent.prototype)。

这两点也就是图中用不同颜色标记的两条线。

  • 3.子类构造函数 Child继承了父类构造函数 Preant的里的属性。使用 super调用的( ES5则用 call或者 apply调用传参)。也就是图中用不同颜色标记的两条线。

看过《JavaScript高级程序设计-第3版》 章节 6.3继承的读者应该知道,这 2和3小点,正是寄生组合式继承,书中例子没有 第1小点。 1和2小点都是相对于设置了 __proto__链接。那问题来了,什么可以设置了 __proto__链接呢。

new、 Object.create和 Object.setPrototypeOf可以设置 __proto__

说明一下, __proto__这种写法是浏览器厂商自己的实现。再结合一下图和代码看一下的 new, new出来的实例的proto指向构造函数的 prototype,这就是 new做的事情。摘抄一下之前写过文章的一段。面试官问:能否模拟实现JS的new操作符,有兴趣的读者可以点击查看。

new做了什么:

  1. 创建了一个全新的对象。

  2. 这个对象会被执行 [[Prototype]](也就是 __proto__)链接。

  3. 生成的新对象会绑定到函数调用的 this

  4. 通过 new创建的每个对象将最终被 [[Prototype]]链接到这个函数的 prototype对象上。

  5. 如果函数没有返回对象类型 Object(包含 FunctoinArrayDateRegExgError),那么 new表达式中的函数调用会自动返回这个新的对象。

Object.create ES5提供的

Object.create(proto,[propertiesObject]) 方法创建一个新对象,使用现有的对象来提供新创建的对象的proto。它接收两个参数,不过第二个可选参数是属性描述符(不常用,默认是 undefined)。对于不支持 ES5的浏览器, MDN上提供了 ployfill方案。 MDN Object.create()

// 简版:也正是应用了new会设置__proto__链接的原理。
if(typeof Object.create !== 'function'){Object.create = function(proto){function F() {}F.prototype = proto;return new F();}
}

Object.setPrototypeOf ES6提供的

Object.setPrototypeOf MDN

Object.setPrototypeOf() 方法设置一个指定的对象的原型 ( 即, 内部 [[Prototype]]属性)到另一个对象或 null。 Object.setPrototypeOf(obj,prototype)

`ployfill`
// 仅适用于Chrome和FireFox,在IE中不工作:
Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {obj.__proto__ = proto;return obj;
}

nodejs源码就是利用这个实现继承的工具函数的。 nodejs utils inherits

function inherits(ctor, superCtor) {if (ctor === undefined || ctor === null)throw new ERR_INVALID_ARG_TYPE('ctor', 'Function', ctor);if (superCtor === undefined || superCtor === null)throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function', superCtor);if (superCtor.prototype === undefined) {throw new ERR_INVALID_ARG_TYPE('superCtor.prototype','Object', superCtor.prototype);}Object.defineProperty(ctor, 'super_', {value: superCtor,writable: true,configurable: true});Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
}

ES6的 extends的 ES5版本实现

知道了 ES6extends继承做了什么操作和设置 __proto__的知识点后,把上面 ES6例子的用 ES5就比较容易实现了,也就是说实现寄生组合式继承,简版代码就是:

// ES5 实现ES6 extends的例子
function Parent(name){this.name = name;
}
Parent.sayHello = function(){console.log('hello');
}
Parent.prototype.sayName = function(){console.log('my name is ' + this.name);return this.name;
}
function Child(name, age){// 相当于superParent.call(this, name);this.age = age;
}
// new
function object(){function F() {}F.prototype = proto;return new F();
}
function _inherits(Child, Parent){// Object.createChild.prototype = Object.create(Parent.prototype);// __proto__// Child.prototype.__proto__ = Parent.prototype;Child.prototype.constructor = Child;// ES6// Object.setPrototypeOf(Child, Parent);// __proto__Child.__proto__ = Parent;
}
_inherits(Child,  Parent);
Child.prototype.sayAge = function(){console.log('my age is ' + this.age);return this.age;
}
var parent = new Parent('Parent');
var child = new Child('Child', 18);
console.log('parent: ', parent); // parent:  Parent {name: "Parent"}
Parent.sayHello(); // hello
parent.sayName(); // my name is Parent
console.log('child: ', child); // child:  Child {name: "Child", age: 18}
Child.sayHello(); // hello
child.sayName(); // my name is Child
child.sayAge(); // my age is 18

我们完全可以把上述 ES6的例子通过 babeljs转码成 ES5来查看,更严谨的实现。

// 对转换后的代码进行了简要的注释
"use strict";
// 主要是对当前环境支持Symbol和不支持Symbol的typeof处理
function _typeof(obj) {if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {_typeof = function _typeof(obj) {return typeof obj;};} else {_typeof = function _typeof(obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;};}return _typeof(obj);
}
// _possibleConstructorReturn 判断Parent。call(this, name)函数返回值 是否为null或者函数或者对象。
function _possibleConstructorReturn(self, call) {if (call && (_typeof(call) === "object" || typeof call === "function")) {return call;}return _assertThisInitialized(self);
}
// 如何 self 是void 0 (undefined) 则报错
function _assertThisInitialized(self) {if (self === void 0) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;
}
// 获取__proto__
function _getPrototypeOf(o) {_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {return o.__proto__ || Object.getPrototypeOf(o);};return _getPrototypeOf(o);
}
// 寄生组合式继承的核心
function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function");}// Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。// 也就是说执行后 subClass.prototype.__proto__ === superClass.prototype; 这条语句为truesubClass.prototype = Object.create(superClass && superClass.prototype, {constructor: {value: subClass,writable: true,configurable: true}});if (superClass) _setPrototypeOf(subClass, superClass);
}
// 设置__proto__
function _setPrototypeOf(o, p) {_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {o.__proto__ = p;return o;};return _setPrototypeOf(o, p);
}
// instanceof操作符包含对Symbol的处理
function _instanceof(left, right) {if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {return right[Symbol.hasInstance](left);} else {return left instanceof right;}
}
function _classCallCheck(instance, Constructor) {if (!_instanceof(instance, Constructor)) {throw new TypeError("Cannot call a class as a function");}
}
// 按照它们的属性描述符 把方法和静态属性赋值到构造函数的prototype和构造器函数上
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);}
}
// 把方法和静态属性赋值到构造函数的prototype和构造器函数上
function _createClass(Constructor, protoProps, staticProps) {if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
}
// ES6
var Parent = function () {function Parent(name) {_classCallCheck(this, Parent);this.name = name;}_createClass(Parent, [{key: "sayName",value: function sayName() {console.log('my name is ' + this.name);return this.name;}}], [{key: "sayHello",value: function sayHello() {console.log('hello');}}]);return Parent;
}();
var Child = function (_Parent) {_inherits(Child, _Parent);function Child(name, age) {var _this;_classCallCheck(this, Child);// Child.__proto__ => Parent// 所以也就是相当于Parent.call(this, name); 是super(name)的一种转换// _possibleConstructorReturn 判断Parent.call(this, name)函数返回值 是否为null或者函数或者对象。_this = _possibleConstructorReturn(this, _getPrototypeOf(Child).call(this, name));_this.age = age;return _this;}_createClass(Child, [{key: "sayAge",value: function sayAge() {console.log('my age is ' + this.age);return this.age;}}]);return Child;
}(Parent);
var parent = new Parent('Parent');
var child = new Child('Child', 18);
console.log('parent: ', parent); // parent:  Parent {name: "Parent"}
Parent.sayHello(); // hello
parent.sayName(); // my name is Parent
console.log('child: ', child); // child:  Child {name: "Child", age: 18}
Child.sayHello(); // hello
child.sayName(); // my name is Child
child.sayAge(); // my age is 18

如果对JS继承相关还是不太明白的读者,推荐阅读以下书籍的相关章节,可以自行找到相应的 pdf版本。

推荐阅读JS继承相关的书籍章节

《JavaScript高级程序设计第3版》-第6章 面向对象的程序设计,6种继承的方案,分别是原型链继承、借用构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承。图灵社区本书地址,后文放出 github链接,里面包含这几种继承的代码 demo

《JavaScript面向对象编程第2版》-第6章 继承,12种继承的方案。1.原型链法(仿传统)、2.仅从原型继承法、3.临时构造器法、4.原型属性拷贝法、5.全属性拷贝法(即浅拷贝法)、6.深拷贝法、7.原型继承法、8.扩展与增强模式、9.多重继承法、10.寄生继承法、11.构造器借用法、12.构造器借用与属性拷贝法。

ES6标准入门-第21章class的继承

《深入理解 ES6》-第9章 JavaScript中的类

《你不知道的 JavaScript-上卷》第6章 行为委托和附录A ES6中的class

总结

继承对于JS来说就是父类拥有的方法和属性、静态方法等,子类也要拥有。子类中可以利用原型链查找,也可以在子类调用父类,或者从父类拷贝一份到子类等方案。继承方法可以有很多,重点在于必须理解并熟 悉这些对象、原型以及构造器的工作方式,剩下的就简单了。寄生组合式继承是开发者使用比较多的。回顾寄生组合式继承。主要就是三点:

  • 1.子类构造函数的 __proto__指向父类构造器,继承父类的静态方法。

    2.子类构造函数的 prototype的 __proto__指向父类构造器的 prototype,继承父类的方法。

    3.子类构造器里调用父类构造器,继承父类的属性。行文到此,文章就基本写完了。文章代码和图片等资源放在这里github inhert和 demo展示 es6-extends,结合 console、source面板查看更佳。

读者发现有不妥或可改善之处,欢迎评论指出。另外觉得写得不错,可以点赞、评论、转发,也是对笔者的一种支持。

关于

作者:常以若川为名混迹于江湖。前端路上 | PPT爱好者 | 所知甚少,唯善学。
个人博客 http://lxchuan12.github.io 使用 vuepress重构了,阅读体验可能更好些
https://github.com/lxchuan12/blog,相关源码和资源都放在这里,求个 star^_^~

微信交流群,加我微信lxchuan12,注明来源,拉您进前端视野交流群

下图是公众号二维码:若川视野,一个可能比较有趣的前端开发类公众号,目前前端内容不多

往期文章

工作一年后,我有些感悟(写于2017年)

高考七年后、工作三年后的感悟

学习 jQuery 源码整体架构,打造属于自己的 js 类库

学习underscore源码整体架构,打造属于自己的函数式编程类库

学习 lodash 源码整体架构,打造属于自己的函数式编程类库

由于公众号限制外链,点击阅读原文,或许阅读体验更佳,觉得文章不错,可以点个在看呀^_^

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/276510.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qt 快速按行读取文件_这是知识点之Linux下分割文件并保留文件头

点击上方"开发者的花花世界"&#xff0c;选择"设为星标"技术干货不定时送达&#xff01;这是一个知识点方便快捷的给结构化数据文件分割大小并保留文件的表头&#xff0c;几十个G的结构化文件不仅阅读编辑麻烦&#xff0c;而且使用受限&#xff0c;因此高效…

mono 调用windows webService

1. 实现linux mono Develop中调用windows 中的webService l linux 与 windows 在一个局域网的网段中 l windows 的IIs中发布webService 2. windows 中的设置 l webService 的代码 using System; using System.Collections.Generic; using System.Linq; using S…

Linux 内存机制

转载链接&#xff1a;http://blog.csdn.net/tianlesoftware/article/details/5463790 一. 内存使用说明 Free 命令相对于top 提供了更简洁的查看系统内存使用情况&#xff1a; [rootrac1 ~]# free total used free shared buffers cached Mem: …

network中的请求信息,headers中的每一项分别是什么意义?

这里是修真院前端小课堂&#xff0c;每篇分享文从 【背景介绍】【知识剖析】【常见问题】【解决方案】【编码实战】【扩展思考】【更多讨论】【参考文献】 八个方面深度解析前端知识/技能&#xff0c;本篇分享的是&#xff1a; 【network中的请求信息&#xff0c;headers中的每…

学习 sentry 源码整体架构,打造属于自己的前端异常监控SDK

前言这是学习源码整体架构第四篇。整体架构这词语好像有点大&#xff0c;姑且就算是源码整体结构吧&#xff0c;主要就是学习是代码整体结构&#xff0c;不深究其他不是主线的具体函数的实现。文章学习的是打包整合后的代码&#xff0c;不是实际仓库中的拆分的代码。其余三篇分…

巴西龟吃什么

1、活虾&#xff0c;哈哈&#xff0c;巴西龟最喜欢的食物&#xff0c;超市很多鸡尾虾买的&#xff0c;就那种&#xff0c;要活的&#xff0c;锻炼它们的天性&#xff0c;一次一只可以吃一、两天&#xff1b; 2、蚶子&#xff0c;贝壳类&#xff0c;活的&#xff0c;整个扔进去&…

绑定dictionary 给定关键字不再字典中_VBA代码集锦-利用字典做两列数据的对比并对齐...

源数据&#xff1a;代码&#xff1a;Sub 对比()Dim arr, brr, crrDim i, j, n, lastrowA, lastrowB As Integer建立字典对象Set d CreateObject("scripting.dictionary")获取数据区域最后一行的行数lastrowA Sheets("对比对齐两列数据").Cells(Rows.Coun…

linux启动时挂载rootfs的几种方式 .

转载链接&#xff1a;http://blog.csdn.net/zuokong/article/details/9022707 根文件系统&#xff08;在样例错误消息中名为 rootfs&#xff09;是 Linux 的最基本的组件。根文件系统包含支持完整的 Linux 系统所需的所有内容。它包含所有应用程序、配置、设备、数据等 Linux 中…

PHP 手册

by:Mehdi AchourFriedhelm BetzAntony DovgalNuno LopesHannes MagnussonGeorg RichterDamien SeguyJakub Vrana其他贡献者2018-06-19Edited By: Peter Cowburn中文翻译人员&#xff1a;肖盛文洪建家穆少磊宋琪黄啸宇王远之肖理达乔楚戴劼褚兆玮周梦康袁玉强段小强© 1997-…

前端使用puppeteer 爬虫生成《React.js 小书》PDF并合并

前端也可以爬虫&#xff0c;写于2018年08月29日&#xff0c;现在发布到微信公众号申明原创。掘金若川 本文章链接&#xff1a;https://juejin.im/post/5b86732451882542af1c80821、 puppeteer 是什么&#xff1f;puppeteer: Google 官方出品的 headless Chrome node 库puppetee…

蜘蛛与佛的故事

最近闭关,空面四壁,窗外层峦叠嶂,窗台上只有一盆花每日陪着我&#xff0c;朋友们都说我要成佛了,想想也是&#xff01; 于是在闭关即将结束的时候找了一篇佛的故事送给自己&#xff0c;希望自己能够顿悟一些"禅"机。 从前&#xff0c;有一座圆音寺&#xff0c;每天都…

信息安全管理与评估_计算机工程学院教师参加“信息安全管理与评估赛项”说明会...

看了就要关注我&#xff0c;喵呜~2019年3月15日下午&#xff0c;2019年陕西省高等职业院校技能大赛“信息安全管理与评估赛项说明会”在咸阳职业技术学院举行。出席本次会仪的有咸阳职业技术学院教务处长杨新宇、神州数码范永强经理、神州数码信息安全工程师高峰和各院校指导教…

haproxy概念和负载均衡

https://pan.baidu.com/s/1Sq2aJ35zrW2Xn7Th9j7oOA //软件百度网盘连接 在80.100虚拟机上 systemctl stop firewalld //关闭防火墙 setenforce 0 //关闭监控 yum install lrz* -y //安装上传软件 tar xf haproxy-1.5.15.tar.gz -C /opt/ //解压压缩包到/opt/ cd /op…

PHP用户注册邮箱验证激活帐号

转载链接&#xff1a;http://www.helloweba.com/view-blog-228.html 本文将结合实例&#xff0c;讲解如何使用PHPMysql完成注册帐号、发送激活邮件、验证激活帐号、处理URL链接过期的功能。 业务流程 1、用户提交注册信息。 2、写入数据库&#xff0c;此时帐号状态未激活。 …

知乎问答:一年内的前端看不懂前端框架源码怎么办?

知乎问答&#xff1a;一年内的前端看不懂前端框架源码怎么办&#xff1f;以下是我的回答&#xff0c;阅读量 1000。现在转载到微信公众号中。链接&#xff1a;https://www.zhihu.com/question/350289336/answer/910970733其他回答的已经很好了。刚好最近在写学习源码整体架构系…

帮自己发个求职简历

帮自己发个求职简历 发个求职信息。本人擅长Web开发&#xff0c;尤其擅长Flex&#xff0c;愿从事Web开发&#xff0c;最好是Web前端开发&#xff0c;下面是我的详细个人简历&#xff1a; 个人信息&#xff1a; 姓名&#xff1a;伍国耀 年龄&#xff1a;23 性别&#xff1a;男 专…

python函数 global_**Python的函数参数传递 和 global

函数的参数到底是传递的一份复制的值&#xff0c;还是对内存的引用&#xff1f;我们看下面一段代码&#xff1a;a []def fun(x):x.append(1)fun(a)print(a)想想一下&#xff1a;如果传递的是一份复制的值&#xff0c;那么列表a应该是不会变化的&#xff0c;还是空列表&#xf…

冷启动问题:如何构建你的机器学习组合?

作为即将告别大学的机器学习毕业狗的你&#xff0c;会不会有种迷茫的感觉&#xff1f;你知道 HR 最看重的是什么吗&#xff1f;在求职季到来之前&#xff0c;毕业狗要怎么做&#xff0c;才能受到 HR 的青睐、拿到心仪的 Offer 呢&#xff1f;负责帮助应届生找到机器学习工作的 …

JavaScript 对象所有API解析【2020版】

写于 2017年08月20日&#xff0c;虽然是2017年写的文章&#xff0c;但现在即将2020年依旧不过时&#xff0c;现在补充了2019年新增的ES10 Object.fromEntries()。发到公众号申明原创。若川顺便在此提前祝大家&#xff1a;2020年更上一层楼。近日发现有挺多人对对象基础API不熟悉…

javascript操作符之new 也疯狂 (2)

JavaScript本是一种基于原形的&#xff08;prototypal&#xff09;语言&#xff0c;但它的“new”操作符看起来有点像经典语言。这迷惑了广大程序员们&#xff0c;并导致了很多使用上的问题。 在JavaScript中&#xff0c;不要用到new Object()这种操作&#xff0c;该用{ }来代替…