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 設計模式
設計模式
   
163
如你所見,
validator
物件是泛用的,保持目前的樣子就足以應付所有檢驗的使用案例,
要增強它只需要新增更多檢查的類型。如果你在許多頁面都使用它來做驗證,很快的你就
可以擁有一整群很棒的特殊檢查。之後當你面對新的使用案例,只需要設定好驗證器並執
validate()
就大功告成了。
外觀模式(Façade
Façade 是一種簡單的模式;它只是為其他物件提供一種替代的介面。保持方法簡短,是一
種很好的設計實踐方式,並且不要讓方法一次處理太多工作。按照這種實踐方式,你最終
將得到一組更大量的方法,這是相對於讓 uber(父物件)的方法帶有大量的參數。有的時
候某兩個或多個方法經常會一起呼叫,在這種情況,建立另一個方法來包裝這些重複出現
的方法呼叫頗有道理。
不需要驗證
歐喔
輔助方法
164
   
第七章
舉例來說,處理瀏覽器的事件時,你有下列方法:
stopPropagation()
困住事件,不讓它向上冒泡(傳遞)到父節點
preventDefault()
不允許瀏覽器進行預設動作(例如,前往一個連結或者提交一份表單)
它們是兩個單獨的方法,有著不同的目的,它們也應當保持獨立,但同時它們又經常一起
被呼叫。所以與其在整個應用中重複這兩個呼叫,你不如建立一個 façade 方法來一次呼叫 ...
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