Skip to Content
JAVASCRIPT網頁應用程式設計
book

JAVASCRIPT網頁應用程式設計

by Alex MacCaw
January 2013
Intermediate to advanced
332 pages
5h 23m
Chinese
GoTop Information, Inc.
Content preview from JAVASCRIPT網頁應用程式設計
34
|
第二章
$("#tabs").trigger("change.tabs", tabName);
});
使用自訂事件,能讓其他程式設計師在擴充我們所開發的程式時,有更多可揮灑的
空間。
DOM 元件無關的事件
以事件為基礎的程式設計方式,威力其實非常強大。因為它能優化應用程式架構、降低
程式間的耦合,幫你開發出功能齊備、易於維護擴充的應用程式。事件並非僅能套用
DOM 元件上,還能讓我們輕易的開發出個人專屬的事件處理函式庫。這稱之為發布
/ 訂閱(Publish/Subscribe)模式(
http://en.wikipedia.org/wiki/Publish/subscribe
),相信它
絕對值得你花些時間一探究竟。
發布 / 訂閱(可簡稱為 Pub/Sub)模式,是一種用於傳遞訊息的模式,主要包含了兩個
角色:發布者和訂閱者。發布者負責將訊息發布於某特定頻道;訂閱者則會訂閱頻道,
並且會在有新訊息發布時接獲通知。
這個模式的重點在於:發布者和訂閱者之間,完全沒有任何耦合關係—雙方彼此完全不
知道對方的存在。雙方都知道的只有一件事:頻道名稱。
只要發布者和訂閱者間不存在耦合關係,在擴充應用程式功能時,就不需花時間搞懂一
堆模組間的相依關係。大大減輕了維護、擴充系統功能時的負擔。
那麼,究竟該如何把 Pub/Sub 模式套用到應用程式上呢?其實只要存好事件名稱 - 事件
處理器兩者間的對應關係,再提供一種呼叫事件處理器的機制就行了。接下來這段範例
程式碼裡面,有個
PubSub
物件,我們可以透過它來新增、觸發事件監聽器: ...
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 第3版 ―ES2015以降の最新ウェブ開発

初めてのJavaScript 第3版 ―ES2015以降の最新ウェブ開発

Ethan Brown, 武舎 広幸, 武舎 るみ
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre
Strange Code

Strange Code

Ronald T. Kneusel
Perfecting Your Thinking Skills

Perfecting Your Thinking Skills

MIT Sloan Management Review

Publisher Resources

ISBN: 9789862766811