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網頁應用程式設計
控制器(Controller)和狀態
|
61
這段程式碼,是把全域變數
jQuery
匯入模組中,並將它取名為
$
。這麼一來,不僅能讓
人一眼看出哪些全域變數需要被模組存取,而且也可加快解譯器找出這些變數的時間。
建議你只要有用到 jQuery 的快捷符號
$
,都一律採用這種做法(
http://docs.jquery.com/
Plugins/Authoring#Getting_Started
),因為這種做法可以確保程式碼不會和其他任何函式
庫相衝突。
匯出全域變數(Global Export
如果想要匯出全域變數,也可以採用類似的做法。其實一般建議最好儘量少在模組裡產
生全域變數,但有時如果真的非這麼做不可,可以先把網頁的
window
物件匯入模組中,
再將欲匯出的變數設定為
window
物件的屬性,讓它匯出成全域變數:
(function($, exports){
exports.Foo = "wem";
})(jQuery, window);
assertEqual( Foo, "wem" );
把用來設定全域變數的變數,取名為
exports
的主要用意,是要讓程式碼更易讀,讓人
可輕易看出哪些全域變數是由模組建立。
關於執行環境(Context)的二三事
有種蠻實用的管理模組方式,是把模組裡的執行環境限制在一塊封閉的區域環境裡(通
常可用於設定事件回叫函式)。預設情況下,模組裡的執行環境應該等同於全域環境—
也就是說,模組裡面的
this
就相當於
window
(function(){
assertEqual( this, ...
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