Skip to Content
精通模块化JavaScript
book

精通模块化JavaScript

by Nicolás Bevacqua
April 2020
Intermediate to advanced
177 pages
1h 45m
Chinese
Publishing House of Electronics Industry
Content preview from 精通模块化JavaScript
5.2
 组合与继承
121
5.2
 组合与继承
接下来,我们将探索如何改进应用程序的设计,不仅仅局限于原生
JavaScript
在语言级别提供的功能。本节将讨论增长代码库的两种不同方法:
继承
通过将代码片段堆叠在一起进行垂直扩展,这样就可以在为他人和自
己定制特性时,可以利用现有的特性。
组合
通过在同一抽象级别添加相关或不相关的代码片段来水平扩展应用程
序,同时将复杂性控制在最低水平。
5.2.1
 通过
Class
继承
ES6
JavaScript
引入用于原型继承的优秀语法之前,原型并不是被广
泛使用的特性。相反,一些工具库提供了辅助方法使继承变得更简单,它
们在底层使用原型继承,但对使用者隐藏实现的细节。虽然
ES6
的类看上
去很像其他语言中的类,但它们是使用原型作为底层的语法糖,所以可以
兼容旧技术和工具库。
引入
class
关键字,再加上
React
框架一开始将类作为声明有状态组件的首
选方法,这些类帮助激发了人们对模式的喜爱,而它以前在
JavaScript
则不太受欢迎。对于
React
,基础的
Component
类提供轻量级的状态管理方
法,同时将渲染和生命周期留给扩展
Component
的使用者类。必要时,使
用者还可以决定实现诸如
componentDidMount
之类的方法,这些方法允许在
挂载组件树之后进行事件绑定。而
componentDidCatch
可用于捕获组件生命
122
5
模块化模式与实践
周期中以及各种其他软接口方法中出现的未处理异常。在基础
Component
类中的任何地方都没有提到这些可选的生命周期钩子 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
基于Python的智能文本分析

基于Python的智能文本分析

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
Lua游戏AI开发指南

Lua游戏AI开发指南

Posts & Telecom Press, David Young

Publisher Resources

ISBN: 9787121387074