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
126
5
模块化模式与实践
5.2.3
 在组合和继承之间进行选择
在实际开发中,除非需要连接到所依赖的特定框架、使用特定的模式(如
扩展原生的
JavaScript
数组),或者当性能被看作最重要的衡量因素时,否
则几乎不需要使用继承。当性能被作为使用原型的一个考量因素时,在匆
忙选择一种可能不那么理想的模式之前,对我们所做的假设进行测试并权
衡不同的方法,是非常有必要的。为了提高性能,我们可能会忽略很多因素。
装饰(
decoration
)和功能组合是更友好的模式,因为它们并不那么死板。
一旦你继承了某些东西,除非继续向原型链添加继承层,否则以后就不能
选择继承其他东西。当多个类继承自一个基类,但需要向外扩展,同时仍
然共享不同的功能部分时,这就成了一个问题。在这些情况以及许多其他
情况下,使用组合能让我们在不牺牲灵活性的前提下挑选所需要的功能。
与简单地改变对象或添加基类相比,函数式的方法实现起来有点麻烦,但
它提供了最大的灵活性。这种方法避免了对底层目标进行更改,使对象易
于序列化为
JSON
,不受不断增长的方法集的影响,因此更容易在代码库中
兼容。
此外,使用基类会使在原型链的不同插入点处重用逻辑变得有点困难。同样,
使用扩展函数,也会使添加类似方法来支持稍有不同的用例变得具有挑战
性。在这方面使用函数方法产生的耦合较少,但它也可能使对象构造的底
层实现变复杂,很难解释清楚它们的功能是如何联系在一起的,这会破坏
我们对代码流的基本理解,并使调试会话的时间变得更长。
与编程中的大多数道理一样,保持外观上的一致性对代码库是有好处的 ...
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