Skip to Content
JavaScript 設計模式
book

JavaScript 設計模式

by Stoyan Stefanov
March 2012
Intermediate to advanced
244 pages
4h 42m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 設計模式
「重複使用程式碼」是一個重要又有趣的主題,因為很自然,就應該盡量寫的精簡,並盡
可能重用現有程式碼,不論這些程式是你或是別人寫的。特別是如果程式碼夠好、經過測
試、容易擴充又有文件化。
當談論到程式碼重用時,第一件映入腦海的事情通常就是繼承,而本章中很大一部分都在
專門探討這個主題。你可以看到數種「class」和非 class 的繼承。但重點是要記得最終的
目的—我們想要重用程式碼。繼承是讓我們達成目的的一種手段,但不是唯一一種。你會
看到如何用物件來組合成其他物件,如何混搭(mix-ins)物件。還有如何用借用的方式來
「只」重用你所需要的功能,但技術上卻不需永久性的繼承。
要進行程式碼重用時,請記住四人幫在書中對於建立物件的建議:「多用物件複合,少用
類別繼承。」
Classical 繼承模式 v.s. Modern 繼承模式
在討論 JavaScript 的繼承相關主題時,你會常常聽到類別繼承(classical inheritance)一
詞,所以讓我們先來了解一下這個 classical 是什麼意思。這個詞在這裡並非傳統或者古典
的意思,也不是指廣為接受的標準方式,其實這只是在 class 這個字上頭作文章罷了。
許多程式語言都有著 class 是物件藍圖的概念。在那些語言中,每個物件都是某個特定的
class 的一個實體(像 Java 就是),而沒有 class 就無法建立出物件。在 JavaScript 中因為
沒有 classes,於是「class 實體」這樣的概念就沒有太大意
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

JavaScript 之美

JavaScript 之美

Anton Kovalyov
重構 JavaScript

重構 JavaScript

Evan Burchard

Publisher Resources

ISBN: 9789862764190