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 設計模式
JavaScript 建立物件很容易,你可以用物件實字,也可以用建構式。而本章我們要更進
一步超越,你將會看到更多物件建立模式。
JavaScript 既簡單又直接,而通常它缺少那些你在別的語言用來描述語言特色的特別語
法,像是命名空間(namespaces)、模組、套件(packages)、private 屬性和靜態成員。
本章會帶領你用常用的模式來實作,或是替代這些語言特色,甚至換個方式重新思考這些
特色。
我們將看到命名空間模式、宣告相依性、模組模式,還有沙盒模式。這些有助於組織、結
構化你的程式碼,並減輕對隱含的全域變數的影響。另外還討論了關於 private 成員、特
權方法、靜態和 private 靜態成員、物件常數、鏈接模式等等主題,還有一個由 class 啟發
的定義建構式的方式。
命名空間模式
命名空間(namespace)可以降低全域變數的需求量,同時可以幫助避免命名衝突和過度
的名稱前
綴詞。
JavaScript 沒有內建的命名空間語法,但這是個相當容易實現的特性。不要用一堆函式、
物件,和變數汙染全域作用域,取而代之,你應該為你的應用程式或函式庫建立一個(且
理想上是唯一一個)全域物件。接著你可以將所有的功能加進該物件中。
物件建立模式
第五章
90
   
第五章
考慮下面的範例:
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