tag:blogger.com,1999:blog-87939143448476465572024-03-08T15:43:04.995+08:00愛德華資訊生活日誌將資訊科技落實於生活運用!My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.comBlogger53125tag:blogger.com,1999:blog-8793914344847646557.post-37998346988338540932018-08-07T18:15:00.001+08:002023-06-28T18:45:25.755+08:00Firefox 網頁黑化擴充套件黑色主題 (或稱夜間模式、深色模式、暗黑模式等) 的網站不但可降低視覺壓力,減少裝置耗電,更能營造一股駭客風格,是近來使用介面設計的流行趨勢。較具代表性的,像是 Youtube 及 Twitter 就各自為自己的網站加入深色模式及夜間模式。<br />
<br />
而對於那些沒有支援黑色主題的網站,則可使用本文介紹的瀏覽器擴充套件來達到網頁黑化效果。以下介紹一些筆者曾經使用或試用過的 Firefox 網頁黑化擴充套件。<br />
<br />
<h2>
<a href="https://addons.mozilla.org/firefox/addon/darkreader/">Dark Reader</a> </h2>
<br />
<div class="separator" style="clear: both; text-align: center;">
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLa34983v1IQVo-WtMcyijmoj8CCaUynNq8eP2tDwVvOjxJP2_oPL-O4D3BuyjWlsTC6tMZQeETsV_Y87ZgjKsx0sXnulSk0cJ4SsKfQ0qdAfE_9c2WQSO8vimx5TCYw5vrQR7LlZVjHehKElmBQS9G_QAeKcVTBUOMEr2j5Zq8w7YMTTiqHS96dU90C4P/s2400/201069%5B1%5D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1500" data-original-width="2400" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLa34983v1IQVo-WtMcyijmoj8CCaUynNq8eP2tDwVvOjxJP2_oPL-O4D3BuyjWlsTC6tMZQeETsV_Y87ZgjKsx0sXnulSk0cJ4SsKfQ0qdAfE_9c2WQSO8vimx5TCYw5vrQR7LlZVjHehKElmBQS9G_QAeKcVTBUOMEr2j5Zq8w7YMTTiqHS96dU90C4P/w400-h250/201069%5B1%5D.png" width="400" /></a></div><br /></div>
<div class="separator" style="clear: both; text-align: center;">
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuTDbUC-DW9QFdTLtcAC6a0kJ1YLpVpzme5Lug8ILxJmzZwXenZe1-48o0QzhhOfnH5Z2iDI3ChIOgrOlyE9C9NhiKmgmVmv3frrf93VQTyJ2E2GwrVX9zourTxYgGzlBKim0qOaRWXFb7n8Rk0DX0rAy6SDgzFqzPmwPlt7DFcFCBylSAvmOWr1RhMP6F/s2400/201070%5B1%5D.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1500" data-original-width="2400" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuTDbUC-DW9QFdTLtcAC6a0kJ1YLpVpzme5Lug8ILxJmzZwXenZe1-48o0QzhhOfnH5Z2iDI3ChIOgrOlyE9C9NhiKmgmVmv3frrf93VQTyJ2E2GwrVX9zourTxYgGzlBKim0qOaRWXFb7n8Rk0DX0rAy6SDgzFqzPmwPlt7DFcFCBylSAvmOWr1RhMP6F/w400-h250/201070%5B1%5D.png" width="400" /></a></div><br /><br /></div>
<div style="text-align: center;">
<br /></div>
在 Windows 底下配色切換效果良好,幾乎大部分的網站不太需要調整就能正常顯示。<br />
<br />
但在 Android 底下不知為何無法正常套用--Ok! 後來查出原因是因為筆者在 Android Firefox 中使用了 User Agent Switcher 套件,更改了瀏覽器的 user agent 字串,而與 Dark Reader 相沖突。不過其實好奇的是背景反黑為何要偵測 user agent 字串啊!<br />
<br />
可以針對每個網站設定套用深色配色、淺色配色,或是不配色。<br />
<br />
配色的演算法內建有三種,還有一種讓您可以對每個網站套用自己的 style,這個功能可用來取代 stylish/stylus 等自訂網頁樣式元件。<br />
<br />
不過可惜的是它的網站客製化 style 只能套用到 domain 層級,似乎是沒法對每個網站的子目錄撰寫自訂樣式。<br />
<br />
<h2>
<a href="https://addons.mozilla.org/firefox/addon/dark-background-light-text/">Dark Background and Light Text</a></h2>
<br />
<div class="separator" style="clear: both; text-align: center;">
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT0fxZzjMrrRF1AjYX927BrrLKnNIUvMp02BVrtKWXfPeQulO9trDQlZJ9xOryPCHniEcYsLB2mrmmqg8wjcVV6KO1kS4SpkFjE0KEJgw6YfsSSFlxtW3NTmr5JCwDKfx5ZhqnC2wGZpRiGD3fQ21YUDGPQt2whT42Qth4xzp6OIjXtJxk2QTVVMlsJ1L8/s533/171089%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="286" data-original-width="533" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT0fxZzjMrrRF1AjYX927BrrLKnNIUvMp02BVrtKWXfPeQulO9trDQlZJ9xOryPCHniEcYsLB2mrmmqg8wjcVV6KO1kS4SpkFjE0KEJgw6YfsSSFlxtW3NTmr5JCwDKfx5ZhqnC2wGZpRiGD3fQ21YUDGPQt2whT42Qth4xzp6OIjXtJxk2QTVVMlsJ1L8/w400-h215/171089%5B1%5D.jpg" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRc-Ax2RZVLabDCZHKln2i4nGUv2trwPc2WqhEWxhofrhn4JvSu4s_QXDGU3E9khghzoJn1fhosXpQx7NgRNKW8Ft8HU6KnxBzOZ7Ur--lDRuVB6fWXb1tuHvCl3Z6YPbYF2ei8SOMlfl_KlOw9Wxq4DEdOI_CcqwhhJLKUGGDln6KwP_LfoOwlergo9rf/s533/171088%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="286" data-original-width="533" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRc-Ax2RZVLabDCZHKln2i4nGUv2trwPc2WqhEWxhofrhn4JvSu4s_QXDGU3E9khghzoJn1fhosXpQx7NgRNKW8Ft8HU6KnxBzOZ7Ur--lDRuVB6fWXb1tuHvCl3Z6YPbYF2ei8SOMlfl_KlOw9Wxq4DEdOI_CcqwhhJLKUGGDln6KwP_LfoOwlergo9rf/w400-h215/171088%5B1%5D.jpg" width="400" /></a></div></div><div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<br />
在更換為 Dark Reader 之前,原本是用這個套件,配色切換的效果也是很好,甚至可能是同類套件中最好的。<br />
<br />
它也能對每個網站選用以下方式客製化:<br />
<br />
<ul>
<li>Disabled - 不使用</li>
<li>Stylesheet processor </li>
<li>Simple CSS </li>
<li>Invert </li>
<li>Default </li>
</ul><p>
<br />
因 Dark Reader 在 Android Firefox 上無法正常執行,目前 Android Firefox 上仍是使用這個外掛。<br />
<br />
要論這個套件有什麼缺點 ,其一是在 Chrome 上沒有相對的元件。<br />
<br />
而其二… 應該是這個元件的 UI 做的比較醜一些,這是比較可惜的地方。</p><p><br />
</p><h2>
<a href="https://addons.mozilla.org/en-US/android/addon/midnight-lizard-quantum/">Midnight Lizard – Add-ons for Android</a></h2>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_0_KrbbYVF1n3reBkmHcVjloSwEgHXp3jFZWoy-ybYLJDKV2FQlLVGObfBYOnB9L28mIgrUU_O68kfn9Rb5T64WOmU8YvUerZn-sRb8MeSoo3W5N9DfQs0jKvjXG2_rLqvKSqU2xQRcf2dr1mCKmVby3zQwvUiChAefTb3JQQJwz4PlmymfUNZh7KzPbB/s533/209132%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="333" data-original-width="533" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_0_KrbbYVF1n3reBkmHcVjloSwEgHXp3jFZWoy-ybYLJDKV2FQlLVGObfBYOnB9L28mIgrUU_O68kfn9Rb5T64WOmU8YvUerZn-sRb8MeSoo3W5N9DfQs0jKvjXG2_rLqvKSqU2xQRcf2dr1mCKmVby3zQwvUiChAefTb3JQQJwz4PlmymfUNZh7KzPbB/w400-h250/209132%5B1%5D.jpg" width="400" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy59UGQnnAEWBebAKOi69LAaji5-bl14cwNcGl6BxcPM_Y8aTXaJE5Sn74dHd3Bx69vUl1abNDNu6Dp6knVGE05E5zVncNMj-E2Hrca0dOPRjbdB87N8ZHitROjSULT115Dk7zY1tlGp3cuoRrBVCJuEY8T4Lmkdeflt7wQJ-9EQJ-sXtxvbUNo05w26FQ/s533/209131%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="333" data-original-width="533" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy59UGQnnAEWBebAKOi69LAaji5-bl14cwNcGl6BxcPM_Y8aTXaJE5Sn74dHd3Bx69vUl1abNDNu6Dp6knVGE05E5zVncNMj-E2Hrca0dOPRjbdB87N8ZHitROjSULT115Dk7zY1tlGp3cuoRrBVCJuEY8T4Lmkdeflt7wQJ-9EQJ-sXtxvbUNo05w26FQ/w400-h250/209131%5B1%5D.jpg" width="400" /></a></div>
<br />
<br />
這曾經是我的最愛。設定畫面漂亮,客製化功能最強的網站樣式修改元件。<br />
<br />
可惜網頁載入速度慢,CPU loading 高,且程式碼 syntax 語法未正常高亮,google calendar 未正常顯示,所以還是捨棄!<br />
<br />
<h2>
<a href="https://addons.mozilla.org/zh-TW/firefox/addon/dark-mode-webextension/">Dark Mode (WebExtension)</a></h2>
<div class="separator" style="clear: both; text-align: center;">
<br /></div><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEB7D5v_RuP_vuPR2roBRLXEPdhtlact6mMmyGR-NNd0yrQ0Abgnipg85gRHZB2TDS45DyOrgfgFp5d6v8xzuyDheKVk9oInOf8PM6SP5cUkGHvxjb4nrTtoDblvVjrJHxrnkzjSUJBqWhsv90MIa_07lBaDMQnsgdnRhyJGTOAugQEbLSZXCG4ebrPL-I/s533/239748%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="333" data-original-width="533" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEB7D5v_RuP_vuPR2roBRLXEPdhtlact6mMmyGR-NNd0yrQ0Abgnipg85gRHZB2TDS45DyOrgfgFp5d6v8xzuyDheKVk9oInOf8PM6SP5cUkGHvxjb4nrTtoDblvVjrJHxrnkzjSUJBqWhsv90MIa_07lBaDMQnsgdnRhyJGTOAugQEbLSZXCG4ebrPL-I/w400-h250/239748%5B1%5D.jpg" width="400" /></a></div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8Wg-seQCEYfHhy09w5xQ9c6z-9IiDSO_l-qm_W3U7ICeogLbqTMlJFsdPPFfidYNYgw7zPnkuLHvc43vvZtRtGegpUTh43STB1LucMMSiBubAuhMfh9h-Fviwsy9bcd4Qnsb8H344sklgknaXOKTDPuxSry19zyHdyHE5vx1kAeqtd-5bV8rG5o8z_EUV/s533/239749%5B1%5D.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="333" data-original-width="533" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8Wg-seQCEYfHhy09w5xQ9c6z-9IiDSO_l-qm_W3U7ICeogLbqTMlJFsdPPFfidYNYgw7zPnkuLHvc43vvZtRtGegpUTh43STB1LucMMSiBubAuhMfh9h-Fviwsy9bcd4Qnsb8H344sklgknaXOKTDPuxSry19zyHdyHE5vx1kAeqtd-5bV8rG5o8z_EUV/w400-h250/239749%5B1%5D.jpg" width="400" /></a></div>
<br />
<br />
這個套件提供很多配色方案讓人選擇,經測試,不同的方案只是呈現的字型或背景色調略有不同,轉換異常的地方,套用不同的配色方案後經常還是異常。<br />
<br />
而且配色方案並不能針對各別網站進行配置,而是所有網站共用同一種。所以假定 A 網站適合用甲方案呈現,而 B 網站適合乙方案呈現,那當由 A 網站切換到 B 網站瀏覽時,還得自行切換相對應的樣式,使用相當不便。<br />
<br />
這個套件提供白名單的機制,讓無法正常套用配色的網站避免反黑,針對個別網站的客製化能力,也僅止於此了!<br />
<br />
<h2>
結論</h2>
目前筆者在桌機上,是使用 Dark Reader,因其 UI 較美觀,轉換效果不錯。但網路上有用戶反應 Dark Reader 會影響網頁載入效能,筆者在使用時,偶爾的確也會遇到瀏覽器提示 Dark Reader 的 Javascript 影響網頁載入速度。但因尚可忍受,也就繼續使用了。Dark Reader 另有 Chrome 版本,是個跨瀏覽器的套件。<br />
<br />
Android Firefox 可使用擴充套件,所以還是可以透過擴充套件使網頁黑化,這是 Chrome Android 版所沒有的功能。在 Android 上筆者是採用 <a href="https://addons.mozilla.org/firefox/addon/dark-background-light-text/">Dark Background and Light Text</a> 這個套件。相較 Dark Reader,它在 Android 中執行效能較高,黑化的效果與 Dark Reader 難分軒輊!<br />
<br />
如果網友想在 Android Firefox 中想使用 Dark Reader 套件,那記得避免使用 User Agent Switch 相關套件,並將 Dark Reader 的預設主題生成模式改成 "過濾+",如此才能使 Dark Reader 正常運作,並得到較佳效能。<br />
<h3>
</h3>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-49262333771992620552014-09-03T23:41:00.000+08:002014-09-04T10:07:00.912+08:00原來使用 Postgresql 就可以直接查詢文字或 CSV 檔案有些時候,我們會需要處理資料內容龐大的文字檔案,我們可能想要先略微檢視資料內容,了解資料分佈,或做些過濾查詢的動作,當然把資料先匯入 Database 再進行處理是一個可行的方案,但如果資料量龐大到數十 GB,那麼光是匯入資料的時間,以及資料匯入 DB 後產生的資料澎漲就足以令人怯步。<br />
<br />
要想維持文字或 CSV 檔案的簡便性,又想擁有資料庫查詢的方便性,可以透過 Postgresql Foreign Table for File 的功能來達成。簡單來說,Postgresql 的 Foreign Data Wrappers 能讓您包裝其他資料來源,然後透過一個集中的資料庫來操作,而這也就是 Federated Database 的觀念。<br />
<br />
<br />
<div style="text-align: center;">
<a href="https://docs.google.com/drawings/d/1z3xYHJVl50ReD0cHIVS3IFlolgb6htkvK6h0Iy7pU2k/pub?w=576&h=248"><img border="0" height="171" src="https://docs.google.com/drawings/d/1z3xYHJVl50ReD0cHIVS3IFlolgb6htkvK6h0Iy7pU2k/pub?w=576&h=248" width="400" /></a></div>
<br />
當我們在 Federated Database 下達 SQL 指令時,中央控管的 SQL 會透過 ANSI SQL 2003 的標準 SQL/MED (Management of External Data),將 SQL 指令進行拆解,交付各個外部資料來源 (或資料庫) 來執行。最後再由中央控管的資料庫,將資料匯集起來傳給使用者。詳情可參考底下兩個連結: <br />
<ul>
<li><a href="https://wiki.postgresql.org/wiki/Foreign_data_wrappers">Foreign data wrappers - PostgreSQL wiki </a> </li>
<li><a href="http://www.slideshare.net/jim_mlodgenski/postgresql-fedaration">Postgresql Federation</a> </li>
</ul>
Postgresql 的 Foreign Data Wrappers 實際上是 Postgresql 資料庫的 Extension,其中 file_fdw 在 Postgresql 9.1 之後的版本已內建,可讓我們把外部的文字或 CSV 檔案,變成是 Postgresql 的 FOREIGN TABLE。筆者進行測試的版本為 Postgresql 9.3 版。<br />
<br />
為了示範,請到 <a href="http://data.gov.tw/node/8792">http://data.gov.tw/node/8792</a> 下載 "每日各站點進出站人數.csv" 檔案。完成下載後將檔名更改為 stationflow.csv。<br />
<br />
接下來以 pgAdmin 執行以下 SQL:<br />
<pre class="prettyprint lang-sql">CREATE EXTENSION file_fdw;
CREATE SERVER file_fdw_server FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE stationflow (
yyyymmdd char(8),
stationcode integer,
stationname varchar(5),
input integer,
output integer
) SERVER file_fdw_server
OPTIONS (format 'csv', header 'true', filename 'D:/testdata/stationflow.csv', delimiter ',', null '');
</pre>
這裡 filename 參數用來指向我們剛剛下載、更名的 stationflow.csv,可能需要修正目錄的正確性。<br />
<br />
注意,目前 pgAdmin 並不會把 FOREIGN TABLE 顯示在左方的樹狀節點中,因此若要查詢系統中有哪些 FOREIGN TABLE, 可以執行以下指令:<br />
<br />
<pre class="prettyprint lang-sql">
select * from information_schema.tables where table_type = 'FOREIGN TABLE'
</pre>
如果在此時執行 SQL, 如:<br />
<pre class="prettyprint lang-sql">select count(*) from stationflow;
</pre>
那應該會收到 Postgresql 提示的 UTF8 編碼錯誤的訊息。這是因為檔案其實是以 Big5 編碼,我們可以使用以下指令修正 FOREIGN TABLE 的檔案編碼:<br />
<pre class="prettyprint lang-sql">ALTER FOREIGN TABLE stationflow OPTIONS ( encoding 'Big5' );
</pre>
完成之後,您便可以正常執行 SQL 操作:<br />
<pre class="prettyprint lang-sql">select count(*) from stationflow;
select * from stationflow where stationname like '台東%';
</pre>
如果你有內部資料表,甚至可以來個內外 Join, Union 或 Sub-Query 等等:<br />
<pre class="prettyprint lang-sql">select * from stationflow where stationcode in (select id from stationlist );
</pre>
測試完成後,可用以下指令刪除外部資料表:<br />
<pre class="prettyprint lang-sql">drop FOREIGN table stationflow;
</pre>
這篇文章僅展示了 Postgresql 查詢文字或 CSV 檔案的功能,事實上 Postgresql 可以外接的資料來源還很多,包括 SQL Database、NoSQL Database、Http、Twitter、Hadoop 等,有興趣的同學可以自行至 <a href="http://pgxn.org/tag/fdw/">PostgreSQL Extension Network</a> 下載編譯測試。
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-84703867663784510792014-06-18T05:36:00.001+08:002014-06-18T05:38:04.759+08:00Java: JSON LibraryBelow is a list of major JSON format processing libraries for java. Check it out.<br />
<br />
<ul>
<li><a href="https://jsonp.java.net/">JSON Processing</a></li>
<ul>
<li>Summary</li>
<ul>
<li>JSR 353: Java API for JSON Processing - Reference Implementation</li>
</ul>
<li>Comment</li>
<ul>
<li>the java version official implementation</li>
</ul>
</ul>
<li><a href="http://www.json.org/java/">JSON in Java (json.org)</a></li>
<ul>
<li>Summary</li>
<ul>
<li>json.org official implementation</li>
</ul>
<li>Comment </li>
<ul>
<li>The API requires you to load the entire document into a String. This is
somewhat cumbersome to use and inefficient for large documents. But it's
a DOM model so you can get a lot done with very little code. </li>
</ul>
</ul>
<li><a href="https://github.com/FasterXML/jackson">Jackson JSON Processor</a></li>
<ul>
<li>Summary</li>
<ul>
<li>High-performance JSON processor.</li>
</ul>
<li>Comment</li>
<ul>
<li>Jackson ones to be the fastest json processing library in the old age.</li>
<li>Has many features with modules approach.</li>
<li>Jackson has grown into a huge collection of data type processing library </li>
</ul>
</ul>
<li><a href="https://github.com/RichardHightower/boon/wiki/Boon-JSON">Boon JSON</a> </li>
<ul>
<li>Summary</li>
<ul>
<li><a href="https://github.com/RichardHightower/boon/wiki">Boon</a> is a opinionated, low ceremony Java framework -- from <a href="https://github.com/RichardHightower/boon/wiki/Birth-of-Boon">Birth of Boon</a></li>
<li>An utility library to simplify java development</li>
</ul>
<li>Comment</li>
<ul>
<li>Claim to be the fastest JSON Library, see:</li>
<ul>
<li><a href="http://rick-hightower.blogspot.tw/2014/01/boon-json-in-five-minutes-faster-json.html">Boon JSON in five minutes, Faster Java JSON parsing, and serialization!</a> </li>
<li><a href="http://www.slideshare.net/richardhightower/java-json-benchmakr">Java JSON Benchmark</a> </li>
</ul>
<li>The JSON parser which has now been ported / forked into <a href="http://glaforge.appspot.com/article/second-beta-for-groovy-2-3">Groovy 2.3</a>. </li>
<li>Built-in path expressions, see <a href="https://github.com/RichardHightower/boon/wiki/Boon-path-expressions">Boon path expressions</a> </li>
</ul>
</ul>
</ul>
<ul>
<li><a href="https://code.google.com/p/google-gson/">google-gson</a> </li>
<ul>
<li>Summary</li>
<ul>
<li>A Java library to convert JSON to Java objects and vice-versa</li>
</ul>
<li>Feature</li>
<ul>
<li>Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa </li>
<li>Allow pre-existing unmodifiable objects to be converted to and from JSON </li>
<li>Extensive support of Java Generics </li>
<li>Allow custom representations for objects </li>
<li>Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types) </li>
</ul>
</ul>
</ul>
<ul>
<li><a href="https://code.google.com/p/json-simple/">json-simple</a></li>
<ul>
<li>Summary</li>
<ul>
<li>JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text. </li>
</ul>
<li>Feature</li>
<ul>
<li>Full compliance with <a href="http://www.ietf.org/rfc/rfc4627.txt" rel="nofollow">JSON specification</a> (RFC4627) and reliable (see <a href="https://code.google.com/p/json-simple/wiki/ComplianceTesting">compliance testing</a>) </li>
<li>Provides multiple functionalities such as encode, decode/parse and escape JSON text while keeping the library lightweight </li>
<li>Flexible, simple and easy to use by reusing Map and List interfaces </li>
<li>Supports streaming output of JSON text </li>
<li>Stoppable SAX-like interface for streaming input of JSON text (learn <a href="http://code.google.com/p/json-simple/wiki/DecodingExamples#Example_5_-_Stoppable_SAX-like_content_handler" rel="nofollow">more</a>) </li>
<li>Heap based parser </li>
<li>High performance (see <a href="https://code.google.com/p/json-simple/wiki/PerformanceTesting">performance testing</a>) </li>
<li>No dependency on external libraries </li>
<li>Both of the source code and the binary are JDK1.2 compatible</li>
</ul>
<li>Comment </li>
<ul>
<li>Can use with JXPath (see below). see <a href="https://sites.google.com/site/yoga80/welcome/jsonxpathandjava">JSON XPath and Java</a></li>
</ul>
</ul>
<li><a href="https://code.google.com/p/genson/">genson - A fast & extensible Java <> Json library</a></li>
<ul>
<li>Summary</li>
<ul>
<li>Genson is an open-source library doing conversion from Java to Json and
Json to Java. Genson targets people who want an extensible but also
configurable, fast, scalable and easy to use library. </li>
</ul>
<li>Feature</li>
<ul>
<li>Easy to use, fast, highly configurable, lightweight and all that into a single small jar! </li>
<li>Full databinding and streaming support for efficient read/write </li>
<li>Support for polymorphic types (able to deserialize to an unknown type) </li>
<li>Does
not require a default no arg constructor and really passes the values
not just null, encouraging immutability. It can even be used with
factory methods instead of constructors! </li>
<li>Full support for generic types </li>
<li>Easy to filter/include properties without requiring the use of annotations or mixins </li>
<li>You can apply filtering or transformation logic at runtime on any bean using the BeaView mechanism </li>
<li><a href="https://code.google.com/p/genson/wiki/JSR353">Genson provides</a> a complete implementation of JSR 353 </li>
<li>Starting with <a href="https://code.google.com/p/genson/wiki/JaxbBundle">Genson 0.95 JAXB annotations</a> and types are supported! </li>
<li>Automatic support for JSON in JAX-RS implementations </li>
<li>Serialization and Deserialization of maps with complex keys </li>
</ul>
<li> Comment</li>
<ul>
<li>Looks good! </li>
</ul>
</ul>
</ul>
Want to use XPath with JSON? see below.<br />
<ul>
<li><a href="http://commons.apache.org/proper/commons-jxpath/">Commons JXPath</a></li>
<ul>
<li>Summary</li>
<ul>
<li>The <tt>org.apache.commons.jxpath</tt> package defines a simple
interpreter of an expression language called XPath. JXPath applies <i>XPath</i>
expressions to graphs of objects of all kinds: JavaBeans, Maps, Servlet contexts, DOM etc,
including mixtures thereof. </li>
</ul>
</ul>
<li><a href="https://code.google.com/p/json-path/">json-path</a> </li>
<ul>
<li>Summary</li>
<ul>
<li>Java JsonPath implementation</li>
</ul>
</ul>
</ul>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-49134151717760530522014-03-27T22:23:00.002+08:002014-03-27T22:24:24.406+08:00Java Practice: Shutdown Hook今日 Daily Java Practice 如下,請問其中 runTime.addShutdownHook 的作用。<br />
<br />
<pre class="prettyprint lang-java">public class Main implements Runnable {
public void run() {
System.out.println("Shutting down");
}
public static void main(String[] arg) {
Runtime runTime = Runtime.getRuntime();
Main hook = new Main();
runTime.addShutdownHook(new Thread(hook));
}
}
</pre>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-57479446016225370642014-03-24T21:25:00.000+08:002014-09-03T23:40:00.685+08:00Java Practice: Enum今天來玩玩 enum, 以下透過一程式,說明 enum 如何把物件導向程式的三個特質封裝、繼承跟多型完美的結合在一起。<br />
善用 enum,可以大幅減少程式碼。<br />
以下請說明:<br />
<ul>
<li>程式輸出訊息。 </li>
<li>為何輸出此訊息。 </li>
<li>S, M, L, XL ... 與 Size 的關係,是類別跟實體的關係,還是父類別跟子類別的關係? </li>
</ul>
<br />
<pre class="prettyprint lang-java">public class SizeIterator {
public static void main(String[] args) {
Size[] sizes = Size.values();
for (Size s : sizes) {
s.showName();
s.showType();
}
}
}
enum Size {
S {
@Override
public void showName(){
System.out.println("I am too small to show to others!");
}
}, M, L, XL, XXL, XXXL;
public void showName() {
System.out.println(this.name());
}
public void showType() {
System.out.println(this.getClass().getName());
}
}
</pre>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-34958387567964260732014-03-19T18:59:00.001+08:002014-03-19T18:59:14.627+08:00Java Practice: Object Serialization今天的 Java Practice 如下, 請服用:<br />
<ol>
<li>Coper 類別的功用為何?</li>
<li>Coper:: <T> copyInstance(T obj) 方法使用了 Java Generic 的技巧,請解釋這裡採用型別參數的好處?</li>
<li>main 方法中印出的兩個 User 物件,內容是否相同? 若有差異,原因為何?</li>
</ol>
<pre class="prettyprint lang-java">abstract class Copier {
@SuppressWarnings("unchecked")
public static <T> copyInstance(T obj) throws IOException,
ClassNotFoundException {
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream(outBytes);
outStream.writeObject(obj);
outStream.close();
ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes
.toByteArray());
ObjectInputStream inStream = new ObjectInputStream(inBytes);
T copied = (T) inStream.readObject();
inStream.close();
return copied;
}
}
class User implements Serializable {
private static final long serialVersionUID = 1L;
private Date date = new Date();
private String username;
private transient String password;
public User(String name, String pwd) {
username = name;
password = pwd;
}
public String toString() {
String pwd = (password == null) ? "(n/a)" : password;
return "logon info: \n username: " + username
+ "\n date: " + date
+ "\n password: " + pwd;
}
}
public class CopierTest {
/**
* @param args
* @throws Exception
* @throws IOException
*/
public static void main(String[] args) throws Exception {
User jack = new User("Jack", "magic");
System.out.println(jack);
User copyOfJack = Copier.copyInstance(jack);
System.out.println(copyOfJack);
}
} </pre>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-14893931381810182402014-03-18T09:08:00.000+08:002014-03-18T09:08:17.403+08:00Java Practice: Random<br />
練習一下使用 Java Random 類別。<br />
以下題目,請試著寫出最後 min, max 印出來的最可能數值。<br />
<pre class="prettyprint lang-java">public class MathRandomTest {
public static void main(String[] args) {
Random rand = new Random();
int n = 10;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < 100; i ++){
int r = rand.nextInt(n + 1);
min = Math.min(min, r);
max = Math.max(max, r);
}
System.out.printf("Min = %d, Max = %d", min, max);
}
} </pre>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-49459755429860297322014-03-17T18:07:00.000+08:002014-03-17T18:07:01.740+08:00Java Practice: Generics從今天開始,預計把發給敝公司新人的 Java Practice 題目,release 在此給大家參考。Java 的初學者可以試著練習看看,老手也可溫故知新喔!<br />
<br />
首先是 Java Generics 之應用,請在 ________ 填入正確宣告,使程式可以正常運作:<br />
<pre class="prettyprint lang-java">class TwoTypeParas <T,V> {
T ob1;
V ob2;
TwoTypeParas(T o1, V o2) {
ob1 = o1;
ob2 = o2;
}
void showTypes() {
System.out.println("Type of T is " + ob1.getClass().getName());
System.out.println("Type of V is " + ob2.getClass().getName());
}
T getob1() {
return ob1;
}
V getob2() {
return ob2;
}
}
public class TwoTypeParaTest {
public static void main(String args[]) {
TwoTypeParas______ tgObj = new TwoTypeParas______(88, "Generics");
tgObj.showTypes();
int v = tgObj.getob1();
System.out.println("value: " + v);
String str = tgObj.getob2();
System.out.println("value: " + str);
}
}</pre>
<br />My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-48824734610705182012013-12-16T12:18:00.001+08:002023-06-28T20:44:05.504+08:00ETL 概念、應用及工具簡介企業裡面常有資料處理、轉換或整合的需求。好的資料管理工具雖然不一定能讓你上天堂,不好的工具卻肯定會讓你住病房。「ETL 資料轉換作業」是「資料管理」中最基礎的應用,本篇就來說說 ETL 的基本概念,它有何應用,以及市場上常見的工具。<br />
<h3>
ETL 是什麼</h3>
<ul>
<li>ETL 代表資料由資料來源端擷取 (Extract) 出來,經過轉換 (Transform),再載入 (Load)到目的端的資料傳輸過程。</li>
<li>Extract -- 從各種不同的資料來源中,將資料擷取出來。傳統上資料來源是來自於交易型資料庫。</li>
<li>Transform -- 對資料進行適當的轉換,如型態轉換、字串相連、彙總運算等等。這個步驟在 ETL 中不一定要執行。</li>
<li> Load -- 將資料載入目的端;目的端通常是為了報表產製及商業智慧分析而最佳化的資料倉儲。</li>
</ul>
<br />
<img height="173" src="https://docs.google.com/drawings/d/e/2PACX-1vTHNveG4yFSt615TmAsohqGtwDbbtm2GckSwzqFPUQRT0-mMkUgSfPmzYVpwZf4lTKu-daTX7Qs6mHh/pub?w=869&h=232" width="640" />
<br />
<h3>
</h3>
<h3>
ETL 的應用情境</h3>
<ul>
<li>同步不同資料庫的資料。</li>
<li>資料清理,移除錯誤資料。</li>
<li>將資料載入資料倉儲以便後續分析, 如進行 OLAP 或 Data Mining 之應用。</li>
</ul>
<h3>
</h3>
<h3>
使用 ETL 工具的優勢</h3>
<h4>
</h4>
使用 ETL 工具最大的優勢,在於可把傳統上需要透過手工方式開發的資料轉換程式碼,轉為由 ETL 工具去進行設定或設計,如此可大量減低資料轉換作業的建置時間及成本。<br />
<br />
目前大部分開發人員還是使用自行開發的指令,來處理資料的整合作業。要透過這種方式來建立商業智慧系統,需要開發人員需具備一定的專業能力,還要費時費力的進行開發及除錯。<br />
<br />
對於資料庫管理人員而言,若不使用外部工具,要連通不同種類的資料來源並不簡單。況且一旦外部資料來源改變,或者要加入新的資料來源,那這種勞心勞力自行開發的工作又要整個重來一次。使用 ETL 工具,將能簡化這種繁複瑣碎的工作。而良好的資料轉換流程之圖形介面設計工具,甚至可將這類操作變成樂趣。<br />
<h3>
</h3>
<h3>
ETL 工具簡介</h3>
以下來說說一些業界比較常用的 ETL 工具:<br />
<h4>
<a href="http://www.informatica.com/us/products/enterprise-data-integration/powercenter/">Informatica PowerCenter</a></h4>
<ul>
<li>Informatica 公司所提供的同名商業化套裝資料整合軟體。</li>
<li>建立於 1993 年,主要業務即為資料整合。</li>
<li>依據 Gartner Dataquest 的報告,它在資料整合領域具有最高的市占率。</li>
<li>它提供了大量的套件供企業進行系統整合,並能連接到大量的新一代或既有的資料來源。</li>
<li><span style="font-family: Arial;">提供良好的作業設計及監控介面,</span>功能強大,但人員必須經過訓練才能上手。</li>
<li>單價昂貴,顧問服務費用高。</li>
<li>無縫整合著名的「下推優化」ELT 方法,使用資料庫的<span style="font-family: Arial;">服務資源</span>來進行資料轉換作業,效能優良,可以應用於大規模的系統。 </li>
</ul>
<ul></ul>
<h4>
<a href="http://kettle.pentaho.com/">Pentaho Kettle (PDI)</a></h4>
<ul>
<li>Pentaho 是一家商業化的公司,約始於 2001 年,他們基於 Java 開發了一套開源 BI 套件,使用者社群人數眾多。 其中包含了一個 ETL 軟體,稱為 Kettle (或稱為 Pentaho Data Integration/PDI)。</li>
<li>透過中介資料趨動 (metadata-driven) 的方式來設計資料轉換流程,而其使用介面功能完善且易於操作。<span style="font-family: Arial;">不需像 Informatica 那樣大的前期投入。</span></li>
<li><span style="font-family: Arial;">其作業設計及轉換使用了兩種腳本文件:Transformation 和 Job。Transformation 進行資料的轉換作業,以 Javascript 撰寫,Job 則用來控制整個作業流程。</span></li>
<li><span style="font-family: Arial;"><span style="font-family: Arial;">具有作業分派的功能 (需要一個像 cron 的分派器),</span>可以透過分散式部署建立 Pentaho 群</span><span style="font-family: Arial;"><span style="font-family: Arial;">集,</span></span><span style="font-family: Arial;">並能運行部署在 slave servers 上的遠程任務。 </span></li>
<li><span style="font-family: Arial;">擁有資料品質管理功能,可以撰寫自定義的 SQL 查詢,Javascript,和正則表達式。 </span></li>
<li><span style="font-family: Arial;">從 4.3 版開始,改採 Apache 授權。參考 </span><a href="http://gigaom.com/2012/01/30/pentaho-goes-apache-route-for-kettle-etl/" id="internal-source-marker_0.5654234683261328"><span data-darkreader-inline-bgcolor="" data-darkreader-inline-color="" style="--darkreader-inline-bgcolor: transparent; --darkreader-inline-color: #56a3f1; background-color: transparent; color: #1155cc; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;">Pentaho changes ETL license for big data push</span></a><span data-darkreader-inline-bgcolor="" data-darkreader-inline-color="" style="--darkreader-inline-bgcolor: transparent; --darkreader-inline-color: #e8e6e3; background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> -- Kettle 4.3, available under the Apache License Version 2.0, can ingest, output, manipulate and report on data from Apache Cassandra, Hadoop HDFS, Hadoop MapReduce, Apache Hive, Apache HBase, MongoDB and Hadapt’s Adaptive Analytical Platform.</span></li>
</ul>
<h4>
<a href="http://www.talend.com/">Talend</a></h4>
<ul>
<li>Talend 是一個開源碼的資料整合軟體, 大概啟始於 2006 年 10 月。</li>
<li>採用程式碼產生器的方式來建立資料轉換作業,它將產生的 Java 或 Perl 程式碼佈署於伺服器執行。</li>
<li>其 GUI 操作介面是建立在 Eclipse RCP 平台之上。</li>
<li>使用者社群人數沒有 Pentaho 多,但在國外有許多商業公司進行支援。像著名的 <span data-darkreader-inline-bgcolor="" data-darkreader-inline-color="" id="internal-source-marker_0.7444855169646989" style="--darkreader-inline-bgcolor: transparent; --darkreader-inline-color: #e8e6e3; background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">Jaspersoft (Jasper Report 開發者)</span> ETL 工具就是採用 Talend ETL。</li>
</ul>
<h4>
<a href="http://www.trinity-etl.com/"><b>Trinity</b></a></h4>
<ul>
<li>Netpro 公司所出產的商業化資料管理整合軟體。</li>
<li>包含 Data Management (即 ETL)、JCS (作業控制系統,用來排程及分派作業),以及 Metadata (管理資料源與資料轉換作業會用到的資料庫、資料表及欄位資訊) 三個部分。</li>
<li>其最新版支援 BigData Management, 能連接到 Hadoop HDFS, Hbase 等 BigData 資料儲存體,亦能執行 Pig 與 HiveQL 操作。</li>
<li>提供以 Web 為基礎的操作介面,資料流程設計工具及作業執行監控工具都能透過 Java Web Start 即時自網頁上即時啟動,整體配套措施相當完善。</li>
<li>採分散式多 Agent 架構,可以簡易的 scale-out.</li>
<li>還包含 Data Profile, Impact Analysis, Data Protection 等各種資料管理軟體,實用性相當高。</li>
</ul>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-9938400386631531522013-02-06T22:31:00.000+08:002013-02-06T22:31:11.134+08:00在 2011 年版的 Mac Mini 上啟動 Linux Mint 的無線網路卡首先執行底下指令,確認自己的無線網路卡是 Broadcom 網路卡:<br />
<br />
<code>inxi -N</code><br />
<br />
其輸出應類似如下,若不是,請另尋它法:<br />
<br />
<pre>Network: Card-1: <span style="color: red;">Broadcom BCM4331 802.11a/b/g/n</span> driver: bcma-pci-bridge
Card-2: Broadcom NetXtreme BCM57765 Gigabit Ethernet PCIe driver: tg3
</pre>
<br />
<ol>
<li>打開 Synaptic 套件管理員,在快速過濾的地方輸入 b43。</li>
<li>將 firmware-b43-installer 及 b43-fwcutter 標記為安裝,如下圖所示。</li>
<li>安裝完成後重新啟動系統,就能正常抓到 Wifi 無線網路了!</li>
</ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-cUTAdhbuWp0/URJoGeaDnwI/AAAAAAAAKHg/g2qfe5cKu_s/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E6%2593%25B7%25E5%259C%2596%25E5%25AD%2598%25E7%2582%25BA+2013-02-06+22%253A20%253A08.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="197" src="http://3.bp.blogspot.com/-cUTAdhbuWp0/URJoGeaDnwI/AAAAAAAAKHg/g2qfe5cKu_s/s320/%25E8%259E%25A2%25E5%25B9%2595%25E6%2593%25B7%25E5%259C%2596%25E5%25AD%2598%25E7%2582%25BA+2013-02-06+22%253A20%253A08.png" width="320" /></a></div>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-49382532707133627662013-01-20T22:47:00.000+08:002013-02-06T22:43:24.045+08:00Windows 8 遠端桌面連線多重登入 + Linux Client<br />
筆者目前在家中的使用的桌機是裝在二手 Mac-Mini 上的 Linux Mint, 雖然這台機器也有 i5 + 8G RAM, 但若想在裡面裝上五、六個 VM 跑 Hadoop, 終究也是力有未逮。<br />
<br />
不過家裡還有另一台硬體配備還算不差的 Windows 8 電腦 (i7-3770 CPU + 32G RAM),這台電腦本是看影片的專用機,但這樣的效能等級拿來看影片簡直是暴殄天物,便動腦筋想要充分運用這台 i7 的效能。 <br />
<h3>
希望效果 </h3>
希望達到的效果是,筆者還是使用 Linux Mint 作為主機,但透過 Windows 遠端桌面連線連到 Windows 8, 而且 Windows 8 的使用者要能 "無感" 於別人正在操作其電腦。要達到這樣的效果,必須達成兩件事: <br />
<ol>
<li>必須讓 Windows 8 專業版可以支援遠端桌面連線多重登入。</li>
<li>讓 Linux Mint 可以透過 Windows 遠端桌面連線連到 Windows 8。</li>
</ol>
<br />
<h3>
讓 Windows 8 專業版可以支援遠端桌面連線多重登入</h3>
要讓遠端電腦可以透過遠端桌面連線連到本機,得先在控制台->系統之中,開啟遠端桌面連線之設定,如 "<a href="http://key.chtouch.com/ContentView.aspx?P=1525">如何開啟 Windows 8 遠端桌面連線?</a>" 所述。 這裡有個重點,要把 "僅允許來自執行含有網路層級驗證之遠端桌面的電腦進行連線" 這個 checkbox <b>取消勾選</b>,才能從 Linux 上連入 Windows 8.<br />
<br />
在 Google 上查詢<span style="font-weight: normal;"> "Windows 遠端桌面連線多重登入", </span>大部分查到的資訊都是 Universal Termsrv.dll Patch 這個方式 (如 <a href="http://itsbox.info/2009/11/23/remove-the-remote-desktop-sessions-limit/">允許遠端桌面多重登入 解除遠端桌面連線多用戶同時連線的限制</a>)。比較可惜的是,這個方式筆者試了一下,在 Windows 8 底下是不 work 的。<br />
<br />
在 Google 大神幫助下,找到了一個 <a href="http://forums.mydigitallife.info/threads/31829-Windows-8-Patcher">Windows 8 Patcher</a> 的方案,只要下載 <a href="http://www.stoned-vienna.com/downloads/Windows%208%20Patcher.exe">Windows 8 Patcher.exe</a>,並以管理員身份執行,重開機之後,便能達到以不同帳號遠端登入,而不會中斷現有使用者之操作之效果。<br />
<h3>
在 Linux 底下透過遠端桌面連線連到 Windows 8</h3>
<br />
這也是靠 Google 大神查來的,透過 <a href="http://www.rdesktop.org/">rdesktop</a> 這套軟體即能達到這個功能。安裝方式,如 "<a href="http://single9.net/2012/10/linux-rdesktop/">Linux下好用的遠端桌面連線程式:rdesktop</a>" 所述,筆者節錄重點如下:<br />
<h4>
安裝方式</h4>
<br />
在命令列下執行:<br />
<blockquote class="tr_bq">
<span id="internal-source-marker_0.23411292092514202" style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">sudo apt-get install rdesktop</span></blockquote>
<h4>
執行方式 </h4>
<blockquote class="tr_bq">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">rdesktop [IP]</span></blockquote>
<h4>
特別參數說明</h4>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">只要在命令列下達 rdesktop 不加任何參數,就會出現其參數說明:</span><br />
<blockquote class="tr_bq">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">rdesktop: A Remote Desktop Protocol client.<br />Version 1.7.1. Copyright (C) 1999-2011 Matthew Chapman et al.<br />See http://www.rdesktop.org/ for more information.<br /><br />Usage: rdesktop [options] server[:port]<br /> -u: user name<br /> -d: domain<br /> -s: shell</span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"><span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;"> …</span> </span></blockquote>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">筆者在命令列裡面是這樣下的: </span><br />
<blockquote class="tr_bq">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">rdesktop -d <u>WINDOWS主機名稱</u> -u <u>使用者名稱</u> -p - -r sound -f <u>WINDOWS主機IP </u></span></blockquote>
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;">其中參數說明如下: </span><br />
<ul>
<li> -u: 使用者名稱</li>
<li> -d: 使用者帳號的 domain,如果不加 -d 參數,則預設會以 MicrosoftAccount 做為登入 domain;MicrosoftAccount 其實就是 MSN 註冊帳號</li>
<li> -p: 密碼 (- to prompt)</li>
<li> -g: 視窗大小 (寬x高)</li>
<li> -f: 全螢幕模式,這個參數不能與 -g 同時使用</li>
<li> -r: 重定向遠端的特定功能,sound 代表要把遠端桌面的聲音重導回本機</li>
</ul>
<h3>
實際效果 </h3>
最後來看看實際的效果,筆者就在 Linux Mint 透過以上命令列連上 Windows 8. 在 Windows 8 中瀏覽 Youtube 網站,試試聲音重導的效果。<br />
<br />
哇哈哈,聲音真的跑到本機上來了。而令人感動的是,Windows 8 那台機器的主人還在看後宮甄嬛傳,影像跟聲音一切正常,完全感覺不到,筆者正在背景裡看 Youtube!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-xVMG31X6EA4/URJr-ik6NeI/AAAAAAAAKHw/gx4Iir8Aek0/s1600/blogtmp2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="197" src="http://2.bp.blogspot.com/-xVMG31X6EA4/URJr-ik6NeI/AAAAAAAAKHw/gx4Iir8Aek0/s320/blogtmp2.png" width="320" /></a></div>
<br />
<br />
對了,現在 Windows 8 遠端桌面連線是在全螢幕模式,該怎麼退出或切換到 Linux 上的其他視窗呢? 原來只要按下 Ctrl + Alt + Enter,就能離開全螢幕模式。<br />
<br />
呃… 是誰說要在裡面裝上五、六個 VM 跑 Hadoop,怎麼這會兒看起 Youtube 了… XDMy Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-86564179747696497452012-12-28T20:01:00.000+08:002012-12-28T20:01:00.669+08:00ETL 與 ESB 之異同與整合應用因為自己開發 ETL 相關產品,加上之前對 ESB 與 SOA 也有接觸,那就來聊聊這兩個東西有什麼關連吧!<br />
<br />
ETL 全名是 <a href="http://en.wikipedia.org/wiki/Extract,_transform,_load">Extract, Transform, Load</a>, 是指資料搬移過程中,從來源資料庫擷取 (Extract)、經過資料轉換 (Transform),再載入 (Load) 到目標資料庫的過程。通常會用商業智慧資料分析的資料準備階段,資料從前端的交易性資料庫,透過 ETL 軟體導到資料倉儲,再交給 OLAP、報表或資料探勘工具做進一步分析。<br />
<br />
而 ESB 全名是 <a href="http://en.wikipedia.org/wiki/Enterprise_service_bus">Enterprise service bus</a>,這是一種企業應用程式的中間層軟體,可以讓不同的應用程式系統,透過 ESB 所提供的開放式介面,彼此介接,達成訊息與應用整合的目標。ESB 是 SOA 服務導向架構在企業部署應用中,比較容易上手的解決方案。打個廣告,個人在 2006 年曾為 ITHome 撰寫的三篇 SOA 專欄文章:<br />
<ul>
<li><a href="http://www.ithome.com.tw/itadm/article.php?c=45039" target="_blank">SOA的開源大勢(1)開放源碼SOA專案低調進入應用階段</a> </li>
<li><a href="http://www.ithome.com.tw/itadm/article.php?c=45135" target="_blank">SOA的開源大勢(2)開源SOA專案的遷徙與合併</a> </li>
<li> <a href="http://www.ithome.com.tw/itadm/article.php?c=45191" target="_blank">SOA的開源大勢(終)開放源碼SOA專案的應用</a></li>
</ul>
<br />
雖然已經有點過時,不過裡面所談到的 ESB 跟 SOA 的觀念,還是具有參考價值。我再找個時間來更新這部分的資料好了! <br />
<br />
好了,談到這裡,那 ETL 與 ESB 有任何關係嗎? 也許有些人會覺得我是不是聯想力太豐富了,才會覺得這兩者有關。嘿! 它們還真的有點關係呢!<br />
<br />
前面所說,ESB 是一種企業應用程式的中間層軟體,用來整合應用,或交換訊息。而事實上,我們也可以把 ETL 看成是另一種中介軟體,但它所介接的兩端,不是應用系統,而是資料來源與目的端。這裡我用中介軟體,而沒講它是 <a href="http://en.wikipedia.org/wiki/Middleware">Middleware</a>, 是因 Middleware 有比較嚴謹的定義。可是實際上,如果從 Middleware 定義:"Middleware makes it easier for <a href="http://en.wikipedia.org/wiki/Software_developer" title="Software developer">software developers</a> to perform communication and <a href="http://en.wikipedia.org/wiki/Input/output" title="Input/output">input/output</a>, so they can focus on the specific purpose of their application." 來講,ETL 軟體也的確具有這樣的功能。<br />
<br />
也就是說,基本上各種中間層軟體,都具有能讓介接兩端的系統,透過中間層軟體介面,達到彼此間獨立演進,又能統合在一起,彼此協作。ESB 是這樣的系統,ETL 其實也有這樣的特色。<br />
<br />
再來談談不同的部分。首先,ETL 通常具有主動性,透過排程,去資料來源抓取資料。但 ESB 通常是被動的,等待應用程式呼叫,才進行訊息的傳輸與 routing. 不過這一點並不絕對,現在的 ETL, 有些也採取 Event Trigger 的方式來設計。例如,當檔案內容發生改變時,觸發 ETL 作業過程。這種作業方式跟 MOM (<a href="http://en.wikipedia.org/wiki/Message-oriented_middleware">Message-oriented middleware</a>) 在收到訊息時進行資料傳輸的行為模式很類似。<br />
<br />
第二點,ETL 所傳輸的資料,通常包含多筆記錄,是屬於 table 或 collection based 的方式在傳輸資料。而 ESB 所傳輸的資料,通常是一個 remote procedure call 或是一個訊息等;是屬於 message 或 document based 的。這點也沒有絕對,因為 ETL 事實上也可以只傳一筆資料,而 ESB 事實上也可以一次傳輸多筆資料。不過就效能上來講,當然 ETL 在傳輸大是資料時,速度應該會較快。這跟架構有關,就先點到為止。<br />
<br />
第三點,是關於服務執行一次轉換或呼叫的時間,英文可稱為 session 吧。通常 ETL 因為一次會傳輸與轉換的量資料,所以執行一次轉換的時間會較久。而 ESB 單一訊息通常資料量不大,因此呼叫時間較短。<br />
<br />
最後則是呼叫或資料傳輸的方式,因為傳統上與 ETL 介接的都是資料庫系統,資料被取出之後,來源資料庫並不會等整個 ETL 過程結束等待回應,再去進行什麼操作。但 ESB 視其中介的性質,可能有不同的呼叫或等待回應方式,例如 SCA 裡面就定義了底下幾種方式:<br />
<ul>
<li><div class="docList">
Request response interaction</div>
</li>
<li><div class="docList">
One-way interaction</div>
</li>
<li><div class="docList">
Conversational interaction</div>
</li>
<li><div class="docList">
Callback interaction </div>
</li>
</ul>
這些應用系統與 ESB 中間層軟件之間豐富的互動性,是傳統 ETL 軟件較缺乏的 (不過傳統上兩者用途本就不同)。<br />
<br />
<br />
前一陣去一某家公司拜訪,他們要導入某大廠的 ESB 系統,做跨事業部門的系統整合。除了各獨立的應用程式要能整合應用外,另一個需求就是以前不同事業單位,分散在各地的區域性資料庫,之後要能進行資料匯整,放到一個中央控管的整合資料庫。這時,若能在規劃中,加入 ETL 的應用,必然會使整個系統的開發更為順利。<br />
<br />
以上情境,當然 ESB 與 ETL 是可以分別採用不同的系統。不過筆者在想的是,住後以雲端為主的應用時代來臨,這兩者都必須要能朝雲端架構演進,甚至統整成為同一個系統,因為以後的應用情境很有很可是前端資料讀取的介面,採用 ESB 的訊息接收模式,而後端則採用 ETL 直接將訊息進行拆解,轉化,載入到 data warehouse 或 hadoop 分析平台之中。因此這兩種應用若能更密切的整合,系統開發跟維護心力就可大幅降低。<br />
<br />
而甚至資料傳輸樣式也超出傳統 ETL 或 ESB 的 message 或 collection based 的方式,而是如新一代的,streaming based 的資料串流,資料從不間斷的產生,永不止息(如 twitter)。如果有一套系統,只要透過組態或圖形化介面方式,就能設計出足以處理這種新的訊息型態的資料流程,那無異是即時訊息處理上的一把神兵利器。且讓我們拭目以待吧!<br />
<br />
<br />
<br />My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-25196389256731810212012-09-14T09:59:00.001+08:002012-09-14T09:59:16.732+08:00你所想像不到的 Javascript
<div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<span style="color: #274e13;">/**</span></div>
<div>
<div style="font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="color: #274e13;"> * 這是一篇我在 N 年前所寫的 Javascript 教材,近來隨著 Node.js 及 Mobile web </span></div>
<div style="font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="color: #274e13;"> * 的熱潮興起,Javascript 又再度走紅,因此再把它重新發佈,希望能對用到的人</span></div>
<div style="font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="color: #274e13;"> * 有所幫助。</span></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<span style="color: #274e13;"> */ </span></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
這不是一篇教你如何在網頁中應用 JavaScript 的教材。這是一篇讓你明瞭看似簡單的 JavaScript,其實其核心語法功能強大。我將在這篇文章中說明 JavaScript 物件導向的特性,並說明如何透過這些特性,達到傳統程式語言 (C++/Java) 所難以完成的功能。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Written by Edward Hsieh/謝鎮澤</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
January 04, 2002 -- 1st Edition</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
January 04, 2009 -- 2nd Edition</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Object and JavaScript</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
一般談到 Object-Oriented,皆會提到物件導向語言必須具有的三個特性,即封裝、繼承及多型。這三種特性中,若缺少了繼承的特性、則稱為 Object-Based。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Javascript 與其說是物件導向語言,倒不如說它是以物件為基礎的程式語言,但它極為動態的程式設計風格,卻能模擬出物件導向的特性。這種作法與大家所熟悉的,以類別 (class) 為基礎的靜態物件導向語言大不相同。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
在 Javascript 中有五種基礎的物件型態,包含 undefined, object, boolean, number, 以及 string。以下的範例可供驗證這五種基礎型態:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (typeof(X));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (typeof(null));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (typeof(true));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (typeof(123));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (typeof('abc'));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
除了上述五種基礎類型,使用者還可自訂型態,而這些自訂的型態,都是「以物件為基礎」的。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Create Objects in JavaScript</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
在 Javascript 中建立物件的方法相當簡單。直接看個例子:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var main = new Object; // 建立新物件</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
main.x = 123; // 設定物件成員變數(屬性)之一</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
main["y"] = "XYZ"; // 設定物件成員變數(屬性)之二</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(main["x"]); // 取得物件屬性並輸出</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(main.y); </div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
可以看到在 JavaScript 中,main.x 與 main["x"] 這兩種語法是通用的。其實在其他語言中,這兩種表示法的語意並不相同。我稍後再作說明。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>List All Members in an Object</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
這是 JavaScript 的必殺技,使用 JavaScript 的人務必要學會這個技巧。底下函式可以傳回一個物件的所有成員的字串表達式,包括物件中的屬性及方法。在物件導向程式設計中,這種技術叫 reflection。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function listMember(main) {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var s = "";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for( key in main ) // 使用 in 運算子列舉所有成員</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
s += key + ": " + main[key] + "\n";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return s;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
範例碼中的 key 會對應到物件中的屬性名稱,如 "x" 或 "y",而 main[key] 則對應到屬性值。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
說這項技巧是必殺技的原因是,你可以透過這項技巧,將物件封裝的黑箱打開來,看看裡面藏有什麼東西。我常用這項技巧來看看 IE 與 Mozilla 的 DOM 物件模型有何不同。試試看下面呼叫範例,就可以知道這項技巧的強大了:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var ary = [123, "abc"];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (listMember(ary));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert (listMember(document.location));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Construct Object with Initial Value</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
要在建立物件的同時指定物件初始值,必須先透過 function 建立一個「原型物件」(或稱為 constructor),再透過 new 運算子建立新物件。例如以下程式碼會建立一個二維陣列的原型,再產生一個新的二維物件。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function Array2DVar(x,y) { // 定義二維陣列原型</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.length = x;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.x = x; // x 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.y = y; // y 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for(var i = 0; i < this.length; i++) // 初始各元素值為 null</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this[i] = new Array(y); // this 代表物件本身</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var a2dv = new Array2DVar(10, 10); // 建立新的 10*10 的二維陣列</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2dv[1][3] = "ABC"; // 設定二維陣列元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2dv[2][6] = "XYZ";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2dv[9][9] = 1000;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2dv[1][3]); // 取得二維陣列元素值,並顯示出來</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2dv[2][6]);</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2dv[9][9]);</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Initial Array Object</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
在 JavaScript 中陣列也是物件 (其實近代多數語言中陣列也都是物件,只有像 C 或 Assembly 這類古老的語言才不把陣列看成物件),因此也可以用 constructor 的語法來建構。當然 JavaScript 還提供了 [] 語法,以更方便建構陣列,範例如下:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a = new Array("abc", "xyz", 1000); // constructor 語法,或</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a = ["abc", "xyz", 1000]; // 陣列標準語法</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
陣列的元素可以是簡單的資料、其他物件,或是函數。舉個例子來在陣列裡面放函式:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
b = [ // 使用函式作為陣列元素</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function () { alert("這個好玩!") },</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function () { alert("再按一次離開!") },</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function () { alert("再來一次!") },</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function () { alert("最後一次!") }</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for (var i = 0; i < b.length ; i++)</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
b[i]();</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
最後一個 for 迴圈是個有趣的應用。由於 b 陣列中現在存放的所有元素都是函式,因此我們可以對 b 的每個元素進行呼叫。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Object as Association Array</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
關連陣列 (Assocation Array) 又稱作 Map 或 Dictionary,是一種物件容器,其中可以放置許多的 key-value pair,以存取子物件。在 JavaScript 中,物件本身就可以作為關連陣列。以關連陣列的方式初始化物件的範例如下:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
obj1 = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"};</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(obj1["a"]); // 顯示 Athens</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
obj2 = {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
name: "Edward",</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
showName: function() { alert(this.name); } // 使用函式作為物件屬性</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
obj2.showName(); // 顯示 Edward</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
obj2.age = 23; // 屬性可以動態加入</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
其中 obj1 儲存了三個子元素,其鍵 (key) 為 "a", "b" 與 "c",而值 (value) 為 "Athens", "Belgrade" 與 "Cairo"。obj2 中 showName 鍵所對應的值為 function,因此 obj2.showName() 即為函式呼叫。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Object as Return Value</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
雖然 Javascript 的函式只能傳回一個變數,但你卻可以將傳回值設定為物件,達到傳回 1 個以上變數值的效果:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function a () {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return [32, 17];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
b = a();</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( b ); // 或</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(a());</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function pixel () { return {"x": 32, "y":17}; }</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
point = pixel (); alert (point.x + "n" + point.y); // 或 alert (pixel().x + "n" + pixel().y);</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Delegation Function Object</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
函式也是物件,只是其中包含的是程式的邏輯。這項特性可拿來作為委任式的程式設計,亦即使用委任函式當作另一函式的參數:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function doloop(begin, end, func) { // 這個函式是個 iterator</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for (var i = begin; i < end; i++) {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
func(i);</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function func1(i) { // 印出 ** n **</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
document.writeln("** " + i + " **");</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
doloop(1, 10, func1); // 印出 1o 行 ** n **</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
doloop(20, 29, function(i) { // 印出 10 行 ## n ##</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
document.writeln("## " + i + " ## ");</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
});</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Object = Properties + Behaviors</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
古有明訓:程式 = 資料結構 + 演算法。而物件是建構程式的基本單位,自然的具有相同的性質。物件除了有屬性 (property),也可具有操作 (behavior),也就是函式。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
假如我們要使用一維陣列來模擬二維陣列,那麼就無法使用 ary[x][y] 這種表示法來設定或取得陣列成員。不過我可以定義一個 set 方法來設定成員變數,而以 get 方法來取得成員變數值。原型函式定義如下:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function Array2D(x,y){ // 以一維陣列模擬二維陣列的原型物件</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.length = x * y; // 陣列總長</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.x = x; // x 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.y = y; // y 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for(var i = 0; i < this.length; i++) // 初始各元素值為 null</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this[i] = null;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.get = function(x,y){ // 成員函式:取得陣列第 [x,y]個元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return this[x*this.x + y];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.set = function(x,y,value){ // 成員函式:設定陣列第 [x,y] 個元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this[x*this.x + y] = value;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
我們接著來使用它:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var a2d = new Array2D(10, 10); // 建立新的「二維」陣列</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2d.set(1, 3, "ABC"); // 設定「二維」陣列元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2d.set(2, 6, "XYZ");</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
a2d.set(9, 9, 1000);</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2d.get(1,3) ); // 取得「二維」陣列元素值,並顯示出來</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2d.get(2,6) );</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert( a2d.get(9,9) ); </div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Member Function Outside of Constructor</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
我們也可以將物件成員函式寫於原型物件之外。以下的 Array2D 物件與上一個範例中的 Array2D 物件有相同的作用,只不過這次是寫在原型物件之外。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function Array2D(x,y){ // 以一維陣列模擬二維陣列的原型物件</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.length = x * y; // 陣列總長</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.x = x; // x 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.y = y; // y 維度長度</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for(var i = 0; i < this.length; i++) // 初始各元素值為 null</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this[i] = null;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.get = Array2DGet; // 用這種方式把成員函式掛進來</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.set = Array2DSet;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function Array2DGet(x,y){ // 成員函式:取得陣列第 [x,y] 個元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return this[x*this.x + y];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function Array2DSet(x,y,value){ // 成員函式:設定陣列第 [x,y] 個元素值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this[x*this.x + y] = value;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Dynamic Object Function</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
這裡說明如何為一個已定義物件,動態的加上其他操作的方法。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
如果一物件已定義完成,而您也使用它來建立了新的物件,這時候您想為原型物件增加新的操作 (而不修改原型物件的原始碼),讓所有該物件的複本都能使用該操作,該如何達成呢?方法是使用物件的 prototype 屬性。以下這個例子,為 Array 這類 Object 在執行期加入一個 max 方法,以取得陣列元素之最大值 (修改自微軟 jscript.chm之範例):</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function array_max(){ // 定義求取 Array 最大值之函式</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var i, max = this[0];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for (i = 1; i < this.length; i++){</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
if (max < this[i])</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
max = this[i];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return max;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Array.prototype.max = array_max; // 在 Array 原型中加入 max 函式</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
上面的程式碼,首先建立一個 array_max 方法,以求取陣列之最大元素。接著將這個方法設定給 Array 原型物件。</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var x = new Array(1, 2, 3, 4, 5, 6); // 透過 Array 建構子建立一陣列</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
// 想求取 x 中某一元素之最大值</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var y = x.max( ); // 取得 x 之最大元素</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Dynamic Mix In</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
假如物件 source 有 mathod1, method2 兩個函式;而另一物件 target 有 methodA 及 methodB 兩個函式。現在我想把 source 的所有特性 (feature) 匯入到 target 中,我們可以撰寫一個 mixin 函式如下,來達到這裡所說的功能:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function mixin(target, source) {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
if( typeof source == "object")</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for( value in source )</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
target[value] = source[value];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
這個 mixin 函式可以動態的為 target 加上另一物件的所有操作。這種 Mix in 的功能可是 C++/Java 的 static type 語言所望塵莫及的。完整的應用範例如下:</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function listMember(main) {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var s = "";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for( key in main ) // 使用 in 運算子列舉所有成員</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
s += key + ": " + main[key] + "\n";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return s;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function main(){ // main 之建構子</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.name = "main";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.methodA = function() {};</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.methodB = function() {};</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var dynamic = {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
method1: function() {},</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
method2: function() {}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function mixin(target, source) {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
if( typeof source == "object")</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
for( value in source )</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
target[value] = source[value];</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
obj = new main();</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
mixin(obj, dynamic); // 匯入 dynamic 物件之所有功能</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(listMember(obj));</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Singleton Object</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
用 associate array 的語法來建立 singleton, 用 function contructor 的語法來建立可多重複製的原型物件</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<b>Inheritance Through Functions</b></div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function superClass() {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.supertest = superTest; //attach method superTest</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function subClass() {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.inheritFrom = superClass;</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.inheritFrom();</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
this.subtest = subTest; //attach method subTest</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function superTest() {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return "superTest";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
function subTest() {</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
return "subTest";</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
}</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
var newClass = new subClass();</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(newClass.subtest()); // yields "subTest"</div>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; color: black; font-family: 微軟正黑體; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
alert(newClass.supertest()); // yields "superTest"</div>
</div>
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-43211353038590224402012-09-14T08:56:00.000+08:002012-09-14T08:56:26.366+08:00一則關於思考的小故事--心裡掛鳥籠一則關於思考的小故事,出自《換個思考,換種人生》第 3 章「精通思考流程」,找個地方思考:
<blockquote>
偉大發明家兼德科公司創辦人凱特林,擁有 140 多項專利,及近 30 多所大學的榮譽博士頭銜,我曾經聽他談起專門闢室思考的事情,他比喻那是在心裡掛鳥籠。<br />
這種說法似乎很怪異,然而當你聽過他以 100 美元跟人打睹的事,就會比較有概念了。<br />
有一回凱特林向朋友說,他有辦法讓對方在來年買一隻鳥當寵物。那位朋友心想,誰也無法「逼」他做這種事,就和他打賭了。<br />
不久之後,凱特林送了一個瑞士手工精製的昂貴鳥龍給這位朋友。朋友把鳥籠帶回家,覺得鳥籠實在美極了,於是掛在飯廰裡。可是他發現每當客人來訪時,總有人問:「你的鳥什麼時候死的?」<br />
「我從沒養過鳥。」他會這麼說,然後把整件事解釋一遍。重複幾次之後,他終於去買了一隻鸚哥,再把輸掉的 100 美元付給凱特林。凱特林後來說:「如果你把鳥籠掛在心裡,終究會放點什麼東西進去的。」<br />
正如凱特林的鳥籠能引來一隻鳥,特定的思考地點也能引來好的想法。如果你來到思考的地點希望能產生好的想法,那麼終究會想出一些好點子。<br />
</blockquote>
這則故事也讓我回想起個人兒時的生活經歷:小學一二年級前,我並不是特別喜歡讀書。有一天父親竟然買了一架簡易式的折疊式書桌,說是給我當禮物。當時我對那書桌的設計感到好奇,也覺得它的樣式精巧。每天吃完晚飯後,非得「享受」一下坐在它上面的「樂趣」不可。就這樣,也養成我至今為止尚稱喜歡閱讀的習慣。My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-300870005010810042012-09-14T08:53:00.000+08:002012-09-14T08:53:23.343+08:00如果這樣的人也能成功的扭轉她的生命在我生活周遭,免不了有些因為生活過得並不很好,而怪罪生活環境的朋友。雖然我認為他們應該停止抱怨,做些更有利於生命的事,但是總是少了一個強而
有力的案例,來說明每個人的確可以因為信念及努力,成功的改變他們的人生,直到我讀到《換個思考,換種人生》,「技巧五:釋放策略思考的力量」,「策略持
家」一節:<br />
<blockquote>
萊恩於 1900 年代中期住在俄亥俄州的迪凡斯。她一直沒有學開車,自從有了孩子以後,也從沒出外工作過。在她那個年代,身為母親就必需留在家裡。這本來也沒有什麼問題,只不過她家裡很缺錢,而且她有 10 個小孩!<br />
她的丈夫凱利在一家機械廠工作,收入十分微薄。更糟的是,凱利是個酒鬼,每星期拿回家的錢,1/3 都被他喝掉了。<br />
任何人在街上遇見萊恩,恐怕都看不出她會運用策略思考,但她的確是。她必需撫養 10 個小孩,打理家務,還要另外賺到足夠的錢養活全家。<br />
在那個時間,製造廠商經常贊助徵稿競賽…〈略過部分〉<br />
萊恩的文字能力不錯,婚前在地方報社工作又鍛鍊了不少,因此她想了一個順理成章的計劃。既然她不能外出賺錢 (即使有人願意照顧 10 個小孩,你能想像這筆褓姆費有多可觀嗎?) 那就參加競賽來賺取金錢與家用品。<br />
想要照顧一家 12 口,洗這麼多人的衣服,還要同時寫數百首詩。廣告歌曲和宣傳短文,需要高度的策略。萊恩做到了。<br />
首先,她苦心建立了一些關係網,用來蒐集參賽表格和購物證明,諸如盒蓋、罐頭標籤等等。其次,她必須在做家事的時候寫東西。每年她都買一本活頁筆記薄,把點子記錄下來,並追蹤一切參賽後的相關資訊。<br />
接下來的挑戰,是找出時間寫作。她處理家務時,總是會把筆記本翻開就近放著,但是她發現燙衣服是最佳時機。<br />
萊恩的策略思考並不限於書寫的準備工作,她也運用策略挑選書寫內容。每一次徵文比賽,她都慎選字眼。她的女兒泰莉回想萊恩準備競賽的情形,說道:<br />
「她
總是會說競賽不只是收集盒蓋和表現聰明才智,還要考慮格式 (有些競賽會要求使用特定字眼,有時若是使用產品想關字眼可以加分)、產品對象
(針對家庭、年輕人或小孩)
和評審。比起贊助商和產品,參賽者更應該重視作為評審的廣告代理商。每個廣告商都有他們的偏好,可能是韻文或散文,可能是幽默或中規中矩的文章。」<br />
萊恩打聽出每家廣告商的喜好,這個策略幫了大忙。在幾年當中,她為這個大家庭贏了幾台洗衣機和烘乾機,數十件大大小小的電器用品、兩輛新車 (車子賣了)、數面筆小額獎金,和兩筆大額獎金,分別是 5000 元與 3400 元。<br />
她把第一筆大額獎金做為頭期款,買下一棟房子,一家 12 口終於搬出 2 房的出租屋。第二筆獎金則用來償還丈夫偷偷申請的二胎房貸 (她一直到償還日前一個月才發現)。<br />
當情況不允許失敗,策略思考就是你最好的選擇。萊恩本來可以偶爾寫首詩,投稿到地方報社去,但她需要做點事來幫忙家人的生計。「像我爸爸這樣的丈夫和父親,永遠不可能改變,」她的女兒說:「我們家唯一的希望就是看媽媽怎麼改變,怎麼撫養一群快樂健康的孩子。」<br />
萊恩成功了。她不僅讓家人活下來,還幫助他們成功。她擁有 7 個小孩大學畢業,1 個拿到博士學位,還有 1 個法學院畢業。<br />
</blockquote>
這則故事給了我很深的感觸。如果在那樣環境下的女人都能因為策略思考而成功,那麼我相信大都數人成功的條件都比她優越的多。我把這樣的故事告訴一個酒鬼朋友,想聽聽看他有何看法。<br />
他說:嗯!能討到這樣的老婆就好了。<br />
嗯,這……也算是策略思考的第一步吧。My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-76540241513863401282011-10-14T21:01:00.000+08:002011-10-14T21:04:45.545+08:00Google+ Bookmarklet<br />
<br />
<a href="javascript:void(location.href=%27https://plusone.google.com/_/+1/confirm?hl=en&url=%27+encodeURIComponent(location.href))">Google+</a> <== 這是 Bookmarklet 連結<br />
使用方式:<br />
<ol>
<li> 將上面這個 Bookmarklet 連結,拉到瀏覽器的網址列,或加入書籤。</li>
<li>在 網址列 或 書籤 中點選剛加入的 Google+ Bookmarklet,就能將目前瀏覽的網頁,分享到 Google+ 上,以下是分享頁面抓圖。</li>
<li> 按下 Share 鈕即可分享。</li>
</ol>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-kXJb15Kc93Q/TpgxbxCuBlI/AAAAAAAAEsk/bOJbUl2eepQ/s1600/Screenshot_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="229" src="http://2.bp.blogspot.com/-kXJb15Kc93Q/TpgxbxCuBlI/AAAAAAAAEsk/bOJbUl2eepQ/s320/Screenshot_1.png" width="320" /></a></div>
<br />
<span id="goog_1905449364"></span><span id="goog_1905449365"></span>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-21877301719650535062011-09-06T21:11:00.001+08:002013-03-28T15:35:13.006+08:00用 Java Annotation 簡化命令列參數處理撰寫 Java 命令列程式,免不了要處理命令列參數。常見的作法是跑個 for loop, 裡面用 if ... else 去判斷要執行的選項。這樣的作法當然可以達到目的,不過比較不具有程式的美感。<br />
<br />
其實在 Web 上,同樣的問題也存在,就是怎麼把 URL 對應到特定的 servlet, action 或 controller 的方法上。這不管是在 Spring Framework 3 Web MVC, Servlet 3 或是 Jessey 中,都有一套近似的解法,就是使用 @Annotation。<br />
<br />
OK, 那我們何不如法泡製,也用 @Annotation 來處理命令列參數呢? 我設計的命令列規範如下:<br />
<ul><li>將命令列參數區分成「選項」及「選項參數」。選項如 <span style="color: red;">-start</span>, <span style="color: red;">-stop</span>;選項參數如 -start <span style="color: red;">localhost 18080</span> 中,未加 dash 符號的字串,即紅字部分。</li>
<li>一個命令列可以接受多個選項及多個選項參數。</li>
<li>支援將選項綁定到主程式裡面的方法,以下我稱之為「選項常式」。</li>
<li>支援將選項參數綁定到選項常式的參數上。</li>
<li>支援自動列出所有選項及其說明。</li>
<li>自動檢查選項參數是否正確。 </li>
<li>支選整數型態的選項參數。</li>
</ul>基於這個規範,我設計出的這組 Command line API,可以透過類似以下方式使用:<br />
<br />
<pre class="prettyprint">package com.isong.cmdexe;
/**
* Usage example:<pre>
* java -jar ds.jar -start localhost 2011
* java -jar ds.jar -start localhost 2011 -sm
* java -jar ds.jar -stop
* </pre>
* @author edwardsayer
*/
public class DummyServer {
/**
* @param args
*/
public static void main(String[] argv) {
new CmdExecutor(new DummyServer()).exec(argv);
}
// execute: java -jar ds.jar -start localhost 2011
@CommandArg(value = "-start ip port", desc = "start the server instance.")
public void doStart(String ip, int port){
System.out.println("Starting server at " + ip + ":" + port + "...");
}
// execute: java -jar ds.jar -sm
@CommandArg(value = "-sm", desc = "start session monitor.")
public void startSessionMonitor(){
System.out.println("Starting session service...");
}
// execute: java -jar ds.jar -stop
@CommandArg(value = "-stop", desc = "stop the server instance.")
public void doStop(){
System.out.println("Stoping...");
}
}
</pre><br />
從以上的程式碼片段,可以知道這組 Command line API 提供了一個 CmdExecutor 類別, 以及一個 @CommandArg annotation。<br />
<br />
@CommandArg 會在主程式的選項常式上,加上標注訊息,包括選項名稱,以及選項描述。<br />
<br />
而CmdExecutor 類別則依據 @CommandArg 所提供的資訊,綁定使用者輸入的參數。另外,若使用者未輸入任何參數,CmdExecutor 則顯示參數提示訊息,這些訊息,其實是我們透過 @CommandArg 加諸在每個選項常式上的訊息。範例如下:<br />
<br />
<pre class="prettyprint">Available options:
-start ip port
start the server instance.
-sm
start session monitor.
-stop
stop the server instance.
</pre><br />
接下來,就來了解 CmdExecutor 及 @CommandArg 的實作方式。先看看 @CommandArg 的程式碼:<br />
<br />
<pre class="prettyprint">@Retention(RetentionPolicy.RUNTIME)
public @interface CommandArg {
String value();
String desc() default "";
}
</pre><br />
可以看到 @CommandArg 包含兩個屬性。value 用來設定選項名稱,而 desc 用來設定選項描述。<br />
<br />
CmdExecutor 較為冗長,程式碼如下:<br />
<br />
<pre class="prettyprint">public class CmdExecutor {
private Map<string method=""> cmdMap;
private StringBuffer cmdDocs;
private Object target;
public CmdExecutor(Object target){
this.target = target;
}
public int exec(String[] args){
System.out.println("args = " + Arrays.asList(args));
initCommands();
if (args.length == 0) {
showCommands();
return 0;
}
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
String option = args[i];
Method method = cmdMap.get(option);
if (method == null) {
System.out.println("Unknown options: " + option);
showCommands();
return 1;
}
try {
Class<!--?-->[] types = method.getParameterTypes();
Object[] paras = new Object[types.length];
for (int j = 0; j < types.length; j++) {
if ( ++i >= args.length || args[i].startsWith("-")) {
System.out.println("Invalid option parameter count: " + option);
showCommands();
return 1;
}
if(types[j].isAssignableFrom(String.class))
paras[j] = args[i];
else if(types[j].isAssignableFrom(int.class)
|| types[j].isAssignableFrom(Integer.class))
paras[j] = Integer.valueOf(args[i]);
else
throw new IllegalArgumentException("Invalid command parameter type: " + method.getName());
}
method.invoke(target, paras);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return 0;
}
private void initCommands(){
if (cmdDocs != null)
return;
cmdDocs = new StringBuffer("Available options:\n");
cmdMap = new HashMap<string method="">();
Method[] methods = target.getClass().getMethods();
for (Method method: methods) {
CommandArg argCmd = method.getAnnotation(CommandArg.class);
if (argCmd != null) {
cmdDocs.append(argCmd.value() + "\n " + argCmd.desc() + "\n");
cmdMap.put(argCmd.value().split(" ")[0], method);
}
}
}
private void showCommands(){
System.out.println(cmdDocs);
}
}
</string></string></pre><br />
以上主要程式邏輯放在 exec 這個方法上,它會透過呼叫 initCommands 方法,建立所有選項常式之描述 (cmdDocs) 及對應 (cmdMap)。完成後,就是實際去解析命令列參數。如果沒有任何參數,就透過 showCommands 顯示說明。如果有,就進行選項與主程式方法的對應。<br />
<br />
<br />
My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-34250205965323942542010-02-28T20:04:00.001+08:002010-02-28T20:08:27.630+08:00用 AutoHotkey 啟用雲端字典及翻譯<p><a href="http://www.google.com.tw/dictionary">Google 字典</a>一直是我相當喜愛使用的網路服務,簡潔的版面及清晰的解釋及例句,都是我喜歡它的原因…硬要說它有什麼缺點的話,就是沒有看到日文的支援。固然透過 Firefox 或 Chrome,我們可以很容易的瀏覽器中啟用翻譯服務,但若是想要在其他應用程式中也使用到 Google 字典,就必須借助其他方法了。</p>
<p><a href="http://www.autohotkey.com/">AutoHotKey</a> 是一個 Windows 下的桌面工具,可以讓我們透過自訂的快速鍵,啟動特定的應用程式、開啟文件、或指定的 URL 等。我們可以利用 AutoHotKey 的功能,在桌面上一鍵啟動 Google 字典及翻譯。</p>
<ol>
<li>下載 <a href="http://www.autohotkey.com/">AutoHotKey</a></li>
<li>執行安裝</li>
<li>安裝完成後,執行 AutoHotKey</li>
<li>在 Windows 的 System Tray 中,在 AutoHotKey 的 [H] 按鈕上按右鍵,帶出右鍵功能表,選擇「Edit This Script」</li>
<li>此時 AutoHotKey 預設會以 Windows 內建的「記事本」(Notepad) 開啟 AutoHotKey.ahk,供我們編輯</li>
<li>接下來就得編輯 AutoHotKey 的指定碼了,所幸相當簡單。其 Help 中有詳細的說明,我把常用的網頁服務設定如下的代碼區塊。</li>
<li>網友們可複製以下程式碼,貼到 AutoHotKey.ahk 檔案最後,然後存檔,關閉記事本。</li>
<li>使用 AutoHotKey 的右鍵功能表,執行「Reload This Script」。</li>
</ol>
<p>至此大功告成,以後在任何應用程式中,只要先選定 (反白) 要查詢的文字,然後按下 Win + X 組合鍵,就可將選定文字帶至 Google 字典中查詢;此外,你也可以使用 Win + S 啟用 Google 搜尋,或使用 Win + Z 啟用 Google 翻譯。</p>
<p>另外,如果你想要讓 Windows 啟動時就自動執行 AutoHotKey.ahk,可以為 AutoHotKey.ahk 建立一個捷徑,並將捷徑放置在開始功能表裡面的「啟動」目錄即可。</p>
<p>Ahk 代碼:</p>
<pre class="prettyprint">
;; 定義按 Win + S 鍵,啟用 Google 搜尋
#s::
clipboard = ; Empty the clipboard
Send, ^c
ClipWait, 2
Run http://www.google.com.tw/search?hl=zh-TW&q=%clipboard%
return
;; 定義按 Win + X 鍵,啟用 Google 字典
#x::
clipboard = ; Empty the clipboard
Send, ^c
ClipWait, 2
Run http://www.google.com.tw/dictionary?aq=f&oq=breed&langpair=en|zh-TW&hl=zh-TW&q=%clipboard%
return
;; 定義按 Win + Z 鍵,啟用 Google 翻譯
#z::
clipboard = ; Empty the clipboard
Send, ^c
ClipWait, 2
Run http://translate.google.com.tw/translate_t#auto|zh-TW|%clipboard%
return
</pre>
<p xmlns="" class="zoundry_raven_tags">
<!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com -->
<span class="ztags"><span class="ztagspace">Technorati</span> : <a href="http://www.technorati.com/tag/AutoHotKey" class="ztag" rel="tag">AutoHotKey</a>, <a href="http://www.technorati.com/tag/dictionary" class="ztag" rel="tag">dictionary</a>, <a href="http://www.technorati.com/tag/goodiesh" class="ztag" rel="tag">goodiesh</a>, <a href="http://www.technorati.com/tag/google" class="ztag" rel="tag">google</a>, <a href="http://www.technorati.com/tag/translation" class="ztag" rel="tag">translation</a></span>
</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-42270493083711306132009-09-15T11:43:00.001+08:002009-09-15T11:48:13.770+08:00WinForm: 如何判斷表單是否開啟在 Visual Studio 的 Designer 中<p>最近工作上用到比較多微軟的開發工具,像是 Visual Studio, Reporting Servoces, ASP.NET 等。</p>
<p>我們採用了一套 UI Framework,可以在 ASP.NET 上,以 WinForm 的程式設計模式來開發 Web 表單。在表單開發上,可完全免除自行撰寫 Client 端 JavaScript 的需要。簡言之,這種開發模式具有 WinForm 的優點,相對的,開發 WinForm 時所需要面對的問題,這裡同樣要面對。</p>
<p>例如,在開發 WinForm 表單時,我們常會在元件的 Load 事件載入資料來源,進行資料綁定。在執行環境這樣的邏輯沒有問題,但在開發環境,以 Visual Studio 的 Designer 開啟元件時,就可能會遇到無法讀取設定檔、資料連結錯誤等等問題,而造成 Designer 無法順利開啟表單。</p>
<p>我查詢了一下,在 <a href="http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/80c6894a-e3b7-4376-8020-43d4ed97b1ee/">MSDN Windows Developer Center</a> 上也有人詢問這個問題,其中有個開發者已經給了一個可用的 solution,但因為他的描述過於複雜,且要求改寫基底類別,所以我擷取了他的解法,加以簡化。</p>
<p>首先,我們撰寫一個 GuiUtil 類別,其中包括下列方法:</p>
<pre class="prettyprint">
public static bool InDesignMode()
{
try
{
return System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv";
}
catch
{
return false;
}
}
</pre>
<p>如此一來,我們就可以採用以下的程式碼,在 Client 端程式的 Load 事件中進行呼叫,而不會產生設計畫面出不來的狀況了。</p>
<pre class="prettyprint">
if (!GuiUtil.InDesignMode())
{
InitDataBinding();
}
</pre>
<p>-- 完 --</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-10056703152469461832009-07-07T09:46:00.001+08:002009-07-07T09:46:45.995+08:00有趣的 MIS 大大<p>剛剛 MIS 大大寄來一封停機公告,重點在那張圖:</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p style="MARGIN: 0cm 0cm 0pt">各位辛苦的系統開發大大們,</p>
<p style="MARGIN: 0cm 0cm 0pt"> </p>
<p style="MARGIN: 0cm 0cm 0pt">這件事情已經拖了一個多月了,實在不一在拖下去了…</p>
<p style="MARGIN: 0cm 0cm 0pt">知道各位都相當的忙碌,不過一個多月的時間應該也足夠才對。</p>
<p style="MARGIN: 0cm 0cm 0pt"> </p>
<p style="MARGIN: 0cm 0cm 0pt">剛剛看過黃曆,7/18 是個好日子,我們會設定在 7/18 子時系統自動關機,之後就不再提供服務了!</p>
<p style="MARGIN: 0cm 0cm 0pt">如果各位還有資料沒有轉到新的測試主機 xx.xxx.xx.xxx 的,請利用這兩週趕快轉移。</p>
<p style="MARGIN: 0cm 0cm 0pt">感謝您的配合!!</p>
<p style="MARGIN: 0cm 0cm 0pt"> </p>
<p><img src="http://farm4.static.flickr.com/3598/3696547160_db0f669e6d.jpg" alt="yellow calendar.png" height="285" width="508"/></p>
</blockquote>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com1tag:blogger.com,1999:blog-8793914344847646557.post-88573833611090099912009-07-04T09:39:00.001+08:002009-07-04T09:48:18.593+08:00Firefox 小技巧--將收藏庫開啟於分頁標籤中<p>Open Firefox Library in a Tab</p>
<p>不曉得有沒有人曾想過,把 Firefox 內的收藏庫 (Library, 管理書籤跟歷史的使用界面),開啟於分頁標籤中,而不是獨立視窗中。</p>
<p>如果你也曾想這麼做,那就這麼做吧 -- 在網址列中輸入以下 URL:</p>
<p>chrome://browser/content/places/places.xul</p>
<p>噹噹…</p>
<p>現在收藏庫便開啟於分頁標籤之中了:</p>
<p><a href="http://www.flickr.com/photos/edwardsayer/3686147092/" title="Flickr 上 edwardsayer 的 Firefox Library in a Tab"><img src="http://farm3.static.flickr.com/2586/3686147092_e530f0f41d.jpg" alt="Firefox Library in a Tab" height="356" border="0" width="500"/></a></p>
<p xmlns="" class="zoundry_raven_tags">
<!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com -->
<span class="ztags"><span class="ztagspace">Technorati</span> : <a href="http://www.technorati.com/tag/bookmark" class="ztag" rel="tag">bookmark</a>, <a href="http://www.technorati.com/tag/firefox" class="ztag" rel="tag">firefox</a>, <a href="http://www.technorati.com/tag/goodies" class="ztag" rel="tag">goodies</a>, <a href="http://www.technorati.com/tag/history" class="ztag" rel="tag">history</a>, <a href="http://www.technorati.com/tag/tab" class="ztag" rel="tag">tab</a></span>
</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com1tag:blogger.com,1999:blog-8793914344847646557.post-43876733388329207232009-06-05T09:01:00.001+08:002009-06-05T09:01:39.372+08:00JavaFX 與 TIOBE Programming Community Index<p><a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE Programming Community Index</a> 是一個用來評量程式語言受歡迎程度的指標,其排名方式,在頁面上說明如下:</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px 'times new roman'; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="TEXT-ALIGN: left; LINE-HEIGHT: 18px; FONT-FAMILY: Arial; COLOR: rgb(102,102,102); FONT-SIZE: 12px" class="Apple-style-span">The index is updated once a month. The ratings are based on the number of skilled engineers world-wide, courses and third party vendors. The popular search engines Google, MSN, Yahoo!, Wikipedia and YouTube are used to calculate the ratings. </span></span></p>
</blockquote>
<p>我大概每個月初都會略微的瞄一下,了解一下目前各主要程式語言的排名狀況。昨天在檢視的時候,發現了 Sun 推了一陣子的 JavaFX 語言,並沒有在排名裡面。可能原因是 JavaFX 並沒有在百大排名裡面,或者是 TIOBE 並沒注意到這個程式語言。而我發現網頁上有這一行字:</p>
<blockquote style="MARGIN-RIGHT: 0px" dir="ltr">
<p><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px 'times new roman'; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"><span style="TEXT-ALIGN: left; LINE-HEIGHT: 18px; FONT-FAMILY: Arial; COLOR: rgb(102,102,102); FONT-SIZE: 12px" class="Apple-style-span">If you have the impression there is a programming language lacking, please notify us at <a style="COLOR: rgb(103,155,195); TEXT-DECORATION: none" href="mailto:tpci@tiobe.com">tpci@tiobe.com</a>.</span></span></p>
</blockquote>
<p>也就是說,如果你發現 TIOBE 可能漏掉某個程式語言的排名,可以寫信告訴他們。因此,我想先確定一下,JavaFX 目前是否有資格擠進百大。我用了一個「比較法」來確認,也就是,如果某語言在 Top 100 或 Top 50,而 JavaFX 的熱門程式與該語言相當,那麼 JavaFX 也該在 Top 100 或 Top 50 左右。</p>
<p>這個用來做比較的語言,最好不要有一字多義的傾向。例如,Ruby、Python、Groovy、Alice 等就都不適合,因為他們不是一字多義,就是菜市場名字。後來我決定以 Erlang 來做比較,比較結果如下:</p>
<ul>
<li>
<div><a href="http://www.google.com/insights/search/#q=javafx,erlang&cmpt=q">javafx vs. erlang</a>: 從 2004 年開始比較。</div>
</li>
<li>
<div>
<p><a href="http://www.google.com/insights/search/#q=javafx,erlang&date=today 12-m&cmpt=q" title="javafx vs. erlang last 12 months">javafx vs. erlang</a>: 近 12 個月的趨勢。</p>
</div>
</li>
</ul>
<p> </p>
<p>就查詢量來看,javafx 有超越 erlang 的趨勢。有了這個發現,我便動筆寫了信給 TIOBE 的成員,而沒想到昨天才寄的信,今早就收到回信。而 TIOBE 也把 JavaFX 納入排名,目前排名第 72。另一個值得小小高興一下的是,TIOBE 的回信者 Paul 在信中說道,為了感謝我的建議,他會在最近一期的排名中公提到我的名字。看看 6 月份的排名,哇,真的有提到我耶 ^_^!</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-83340169583186044312009-05-05T22:15:00.001+08:002009-05-05T22:15:02.212+08:00有趣的 Ruby 字串操作<p>irb(main):025:0> a= "abc def"
<br id="od-8"/>
=> "abc def"
<br id="od-80"/>
irb(main):026:0> a["def"] = "abc"
<br id="od-81"/>
=> "abc"
<br id="od-82"/>
irb(main):027:0> a
<br id="od-83"/>
=> "abc abc"
<br id="od-84"/>
irb(main):028:0> a["abc"] = "xyz"
<br id="od-85"/>
=> "xyz"
<br id="od-86"/>
irb(main):029:0> a
<br id="od-87"/>
=> "xyz abc"
<br id="od-88"/>
irb(main):030:0> a["abc"]
<br id="od-89"/>
=> "abc"
<br id="od-810"/>
irb(main):031:0> a["def"]
<br id="od-811"/>
=> nil
<br id="od-812"/>
irb(main):032:0>
<br id="od-813"/>
<br id="od-814"/>
<span style="FONT-FAMILY: Verdana" id="od-815"><span style="FONT-SIZE: 0.75em">date <strong>=</strong> "2009/12/12"
<br/>
month, day, year = date.split('/')
<br id="od-817"/></span></span></p>
<p xmlns="" class="zoundry_raven_tags">
<!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com -->
<span class="ztags"><span class="ztagspace">Technorati</span> : <a href="http://www.technorati.com/tag/programming" class="ztag" rel="tag">programming</a>, <a href="http://www.technorati.com/tag/ruby" class="ztag" rel="tag">ruby</a></span>
</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-5089273446535129842009-04-23T09:28:00.002+08:002009-05-12T22:10:00.916+08:00C # 裡面 ?? (雙問號)運算子的妙用<p>?? 運算子是在 2.0 版後新增的,至今我在別人的程式中還很少看到有在使用。
<br/>
它的使用方式如下:</p>
<p>a = b ?? c;</p>
<p>上式的意思是,如果 b != null,那就把 b 的值指定給 a,否則就把 c 的值指定給 a。
<br/>
照理講,?? 的功能完全可以由 ?: 運算子替代,如上式可以改寫如下:</p>
<p>a = b != null ? b : c;</p>
<p>不過,?? 對於簡化程式碼很有幫助,我相信許多 one liner 會很喜歡它,例如:</p>
<p>if (Session["WEB_TYPE"] == null)
<br/>
{
<br/>
Session.Add("WEB_TYPE", "");
<br/>
}</p>
<p>可以改寫為:</p>
<p>Session["WEB_TYPE"] = Session["WEB_TYPE"] as string ?? "";</p>
<p>?? 的另一個特點是使用上可以串連,例如:</p>
<p>string a = null;
<br/>
string b = null;
<br/>
string c = "c";
<br/>
string d = a ?? b ?? c;</p>
<p>這樣的意思,也就是由左到右,找到第一個非 null 值的變數,指定給 d。</p>
<p>於是,如果有一段程式如下 (抓取系統中的舊程式碼舉個例子):</p>
<p>if (Session["WEB_TYPE"] == null)
<br/>
{
<br/>
Session.Add("WEB_TYPE", "");
<br/>
}</p>
<p>//接收 Web_type
<br/>
if (!IsPostBack)
<br/>
{
<br/>
if (Request.QueryString["WEB_TYPE"] != null)
<br/>
{
<br/>
webtype = Request.QueryString["WEB_TYPE"].ToString();
<br/>
Session["WEB_TYPE"] = webtype;
<br/>
}
<br/>
}</p>
<p>可以用 ?? 運算子改寫成以下版本:</p>
<p>Session["WEB_TYPE"] = Session["WEB_TYPE"] as string ?? Request.QueryString["WEB_TYPE"] ?? "";</p>
<p>這樣程式就精簡許多,也更好理解與維護了。</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0tag:blogger.com,1999:blog-8793914344847646557.post-28536902908662783702009-04-22T10:17:00.001+08:002009-04-22T10:18:34.645+08:00在麥當勞看了一場戲<p>一個皮膚黝黑的男子,兩眼無神的呆坐在餐飲區的一角。
<br/>
一個體格壯碩的男子帶著一個清瘦的女生走了進來,坐在黝
<br/>
黑男子的身旁,對他交待了幾句,拍拍了那女生的肩,便走
<br/>
開了。</p>
<p>那男的哭喪著臉,哀求的詢問著那女生,那女生正顏厲色的
<br/>
反過來詰問那個男的… 那男的無言以對,熱淚涔涔而下,
<br/>
這種狀況來回幾次,那男的奪門而出。</p>
<p>那個壯碩的男子回來,再度拍拍那女生的肩,安慰她幾句,
<br/>
再度離去把那女生留在現場。</p>
<p>最後,另一個面容白晰的男生走了進來,拉著那女生的手,
<br/>
摟了摟那女生的肩,聊了幾句。那女生嘆了口氣,如釋重負
<br/>
般的站了起來,那男生也跟著站了起來,兩個人牽著手,漫
<br/>
步離開現場。</p>My Bloghttp://www.blogger.com/profile/13637533567636695411noreply@blogger.com0