<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Laura Lee on Medium]]></title>
        <description><![CDATA[Stories by Laura Lee on Medium]]></description>
        <link>https://medium.com/@l443018?source=rss-4ffd4a89a178------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vNW9mOd6xlK5EEtlBExrnw.png</url>
            <title>Stories by Laura Lee on Medium</title>
            <link>https://medium.com/@l443018?source=rss-4ffd4a89a178------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:25:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@l443018/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Object-Oriented Design Principles — SOLID]]></title>
            <link>https://medium.com/@l443018/object-oriented-design-principles-solid-8d8637501bb?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d8637501bb</guid>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[solid]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Wed, 20 Mar 2019 09:53:28 GMT</pubDate>
            <atom:updated>2019-03-20T10:02:40.684Z</atom:updated>
            <content:encoded><![CDATA[<h3>Object-Oriented Design Principles — SOLID</h3><p>這篇文章蒐集並整理了網路上的資源，從前端開發觀點，討論物件導向設計原則：SOLID<br>Single responsibility principle<br>Open–closed principle<br>Liskov substitution principle<br>Interface segregation principle<br>Dependency inversion principle</p><p>其實五個原則講的東西都是同一件事：<strong>如何好好的定義並且繼承一個類別 ，讓它變得好維護、好管理，</strong>只是從不同的切入角度觀察。但理想很美滿，現實很骨感，常常為了求快，在開發過程中這些原則被忽視或是捨棄掉了。所以好的開發流程應該是在工作有餘的時間，努力依照原則進行重構，解決技術債，才不會越欠越多。</p><p>前端越寫越像是後端一樣......但當初大二學的 Java 早就忘光光orz，只能重新再來學習。開發者的世界真的是合久必分，分久必合</p><h4>Single Responsibility</h4><p>每個class處理的工作要夠小夠單一 （好像可以跟 atomic 概念結合)<br>參考這邊文章的範例：</p><p><a href="http://melomelo1988.pixnet.net/blog/post/293517340">OCP-Open Closed Principle(開放封閉原則) @ melo 唐 :: 痞客邦 ::</a></p><p>它在 BankProcess 裡面處理超過一種銀行業務 process，應該要拆分出來到更底層的 class</p><h4>Open-Closed Principle</h4><p>open for extension, but closed for modification<br>要可以擴充但不能修改原本的內容</p><p>呈上面的範例，設定單一抽象界面 IBankProcess ，不實作、只定義，就可以方便擴充。每當有新的 Process 出現，就再另外定義 Process 並繼承 IBankProcess，各自使用不同的實作方式，而不影響到原本的 parent / interface</p><h4>Liskov Substitution Principle</h4><p>若有一父類別 A, 跟子類別 B 則所有使用 A的地方可以用B取代而不影響運作<br>（即子類別不應該有擅自改掉父類別行為的定義，子類別只能擴充，不能改原則）</p><p><a href="https://medium.com/@f40507777/%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8F%9B%E5%8E%9F%E5%89%87-liskov-substitution-principle-adc1650ada53">里氏替換原則 Liskov Substitution Principle (LSP)</a></p><p>如上範例，正方形擅自改掉繼承的面積計算方法(長x寬)變成(寬x寬) 這樣是不合適的，如果實作的時候跟父類別不一致，就失去繼承的意義了。需另外定義自己客製化的 method 才對。（但常常為了求快求方便一直 override parent 導致程式邏輯出錯阿）</p><h4>Interface Segregation</h4><p>父類別的定義要夠嚴謹。如果子類別有不支援父類別的情況，則表示父類別定義可能不夠抽象或是太過特定，要再把其中 method 分離出來，子類別另外繼承其他 method。</p><p><a href="https://ithelp.ithome.com.tw/articles/10192464">SOLID 之 介面隔離原則（Interface segregation principle） - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天</a></p><p>上面範例提到，在鳥類中實做飛行的方法，身為繼承鳥類的企鵝非常尷尬…所以應該要把飛行另外做一個 class 來處理（瞬間有種生物學介門綱目科屬種的既視感）</p><h4>Dependency-Inversion Principle</h4><p>反轉依賴，不是讓上游去依賴下游模組，而是讓底層與上層都相依於上層規劃好的抽象介面</p><p><a href="http://teddy-chen-tw.blogspot.com/2012/01/5dependency-inversion-principle.html">亂談軟體設計（5）：Dependency-Inversion Principle</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/0*RYatgYfU5VhfQse-.jpg" /><figcaption>這篇文章範例，把五個原則裡面的三個原則都包含進去了，推薦觀看</figcaption></figure><p>在上層規劃好自己想要達成的目標，由下層接上這個目標來實做，program to an interface, not an implementation（和上方提到的定義 interface 方便擴充，有異曲同工之妙）</p><p>把處理檔案這件事情（這個責任）抽離出來定義成 interface -- IFileManipulate ，讓上層的 Policy Module 要處理檔案的時候只使用 IFileManipulate 介面所提供的服務，不要直接操作底層 IO。底層 Mechanism Module 可以提供多個 IFileManipulate 介面的實作方法，例如可以透過 File、HTTP、FTP 等通訊協定來操作檔案的實做。如此一來整個系統就變得比較有彈性且容易擴充。</p><p>這範例不只是 Dependency-Inversion Principle，還符合以下兩個原則：</p><p>- 符合 Open-Closed Principle：如果要擴充不同的檔案同步機制，只要在 Mechanism Module 中加入新的 IFileManipulate 介面的實作就可以了（open for extension），不需要去修改原本已經可以正常運作的程式碼（closed for modification）</p><p>- 符合 Liskov Substitution Principle，確保底層的實作會遵守上層介面所定義的行為。</p><p>也就是說，設計一個功能時，應要建立<br>- 抽象的想達成的願景或目標（盡量不可變動）<br>- 在實作層運行 method，要完成目標可以有很多不同的方法，要留有可變動的彈性</p><p>但最難的就是那個 &quot;抽象的願景&quot;，因為一開始寫程式通常都會先求有、再求好，不太可能一開始就能把產品抽象化，往往都是寫到後來才發現有可以抽出來的 pattern。重構實在是很博大精深的學問阿</p><h4>Reference</h4><ul><li><a href="http://melomelo1988.pixnet.net/blog/post/293517340">OCP-Open Closed Principle(開放封閉原則) @ melo 唐 :: 痞客邦 ::</a></li><li><a href="https://medium.com/@f40507777/%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8F%9B%E5%8E%9F%E5%89%87-liskov-substitution-principle-adc1650ada53">里氏替換原則 Liskov Substitution Principle (LSP)</a></li><li><a href="https://ithelp.ithome.com.tw/articles/10192464">SOLID 之 介面隔離原則（Interface segregation principle） - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天</a></li><li><a href="http://teddy-chen-tw.blogspot.com/2012/01/5dependency-inversion-principle.html">亂談軟體設計（5）：Dependency-Inversion Principle</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d8637501bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera website UX case study — Navigation]]></title>
            <link>https://medium.com/uwillx/coursera-website-ux-case-study-navigation-80bb2f671170?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/80bb2f671170</guid>
            <category><![CDATA[case-study]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[ux-research]]></category>
            <category><![CDATA[coursera]]></category>
            <category><![CDATA[navigation]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Sun, 20 May 2018 15:10:20 GMT</pubDate>
            <atom:updated>2018-05-20T15:10:46.470Z</atom:updated>
            <content:encoded><![CDATA[<h4>Coursera 課程網站使用後的想法與讚賞 (圖多)</h4><p>最近開始對UX有興趣，於是就找了coursera 上面的 UX 課程來學習 :D</p><p>一開始沒用過這個網站，花了點時間研究網頁功能和上課規則，過程中發現蠻有趣的一些點，像是網站上方導覽列極簡到幾乎沒有東西，還有明明有付費課程，卻沒有太多行銷廣告等等。決定以這個網站作為第一次 UX 分析的練習，文章有三個區塊：</p><ol><li>Coursera 網站主要頁面 highlight</li><li>了解 Jakob Nielsen 提出的 Navigation UX 原則</li><li>將 Navigation 原則與各個網站頁面對照，評比網頁那些地方符合原則、那些不符合；原則適用的情境；以及網頁有哪些地方可以改善。</li></ol><p>由於一開始是對 Coursera 極簡風的導航感到新奇，所以決定聚焦 Navigation 的 UX 觀察，但也會提到 UI 層面。</p><h4>網站頁面 highlight</h4><p><strong>首頁</strong><br>上方橫幅沒有直接推播課程，要捲到下面才看的到熱門課程輪播 (carousel)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mVntPv1quE_4Iy3aPf8FEg.png" /></figure><p><strong>登入後的首頁</strong><br>變成功能 panel，畫面依然簡潔，甚至連課程推薦都要自己點到 &quot;Recommendations&quot; 才看的到，第一次點 “Recommendations” 進去網頁會請使用者選擇有興趣的類別，他會推薦相關課程</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fybfQycv2ItTCiO7rmQQEA.png" /></figure><p>這裡還蠻疑惑怎麼沒把課程推薦放在畫面底部，感覺有意營造一種非營利、學術性的風格。</p><p><strong>課程介紹</strong><br>簡單好懂，但免費課程（左圖）沒有標註 Free，付費課程（右圖）也沒有標註價格</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EWy_5IOHyJnVVMzLb1EbPA.png" /></figure><p>這裡忍不住要吐槽，第一次點進課程看到左邊畫面，會不敢點選參加，怕一不小心就買了課程 XD 後來才發現付費課程，會像右圖用 Try for Free 標示，真的相當隱晦的作法。</p><p>覺得在文案部分可以再改善，譬如 Enroll for Free 之類的，不太明白 Coursera 的考量是甚麼？目前不清楚他的課程付費機制，定價機制等等，可以當作下次的研究題材。</p><p>跟 Udemy 比較，兩者取向相當兩極化。Udemy 極盡商業化，廣告多，標價清楚且是浮動價格，會進行促銷。Coursera 課程偏向學校教授的學術研討，而 Udemy 偏向業界技能實戰。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*WHqamx5H9JxrdOzpTJqfrg.png" /><figcaption>單純做個氣氛情境的對照組，並非這次討論範圍</figcaption></figure><p><strong>課程介紹 (Enroll 後)</strong><br>會著重在目前上課進度，而非課程內容</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CnGo3VaG3WEm5VklCLcVHg.png" /></figure><p>以上是這次討論涵蓋的 Coursera 主要功能頁面。</p><h4>Navigation UX 原則</h4><p>接下來要導入 Navigation UX 原則來分析這次的案例，來源為 &lt;Is Navigation Useful?&gt; — Jakob Nielsen。</p><p>如果想直接看網站與原則的分析，可跳過這個區塊，先看第三段文章。</p><p><a href="https://www.nngroup.com/articles/is-navigation-useful/">Is Navigation Useful?</a></p><p>Jakob 首先提出八點使用者的行為（附上 Google 中文翻譯）：</p><p>A1. users comment on the content first; if the content is not relevant, then they don’t care about any other aspect of the design<br>用戶首先評論內容; 如果內容不相關，那麼他們不關心設計的任何其他方面</p><p>A2. when they arrive on a page, users ignore navigation bars and other global design elements: instead they look only at the content area of the page<br>當他們到達一個頁面時，用戶會忽略導航欄和其他全局設計元素：相反，他們只查看頁面的內容區域</p><p>A3. users don’t understand where they are in a website’s information architecture<br>用戶不明白他們在網站的信息架構中的位置</p><p>A4. users are extremely goal-driven and look only for the one thing they have in mind — they don’t spend much time on promotions for anything else<br>用戶是極其目標驅動的，並且只考慮他們想到的一件事 — 他們不會花太多時間在其他任何促銷活動上</p><p>A5. in pursuit of their goal, users often rely on search as their main hunting strategy<br>為了追求自己的目標，用戶經常依靠搜索作為他們的主要狩獵策略</p><p>A6. users rarely look at logos, mission statements, slogans, or any other elements they consider fluff (in particular, they ignore advertising and anything that looks like an ad)<br>用戶很少看標識，使命宣言，標語或他們認為絨毛的任何其他元素（特別是，他們忽略廣告和任何看起來像廣告的東西）</p><p>A7. if a page does not appear relevant to the user’s current goals, then the user will ruthlessly click the Back button after two to three seconds<br>如果一個頁面與用戶的當前目標不相關，那麼用戶將在兩到三秒後無情地點擊後退按鈕</p><p>A8. if users don’t understand a certain design element, they don’t spend time learning it — instead, they ignore it and continue the hunt for their own goal<br>如果用戶不理解某個設計元素，他們不會花時間學習它 — 相反，他們忽略它並繼續尋找自己的目標</p><p>再來 Jakob 提出了設計原則（僅節錄部分）：</p><p>Generic Links</p><p>B1. Restrict linking to basic features (say, search, copyright, and a few more), the home page, and the main page for the current section.<br>縮減鏈接到剩下基本功能（如搜索，版權等），主頁和當前部分的主頁面。</p><p>B2. Less is more : having a small number of standard links on every page makes it more likely that users will notice those links they do need.<br>少即是多 ：每個頁面上都有少量的標準鏈接，使用戶更有可能注意到他們需要的鏈接。</p><p>Structural Navigation</p><p>B3. Do not link to all sections of the site from all pages. What is the probability that a user will go from looking at hairdryers to looking at grunge music? … Why not just go back to the home page …<br> 不要鏈接到所有頁面的網站的所有部分。用戶從看吹風機看看垃圾音樂的可能性有多大？（筆者註：子頁面幾乎不會有互相連結的需要）… 為什麼不回到主頁</p><p>B4. … provide links to all levels of the hierarchy above the current location. … Without structural links, pages become orphans that are not contextualized. And since users often arrive at pages through search or other means that bypass the higher-level navigation pages, it is necessary to provide a path back to these higher levels.<br>提供指向當前位置上方所有級別的鏈接。… 沒有結構鏈接，頁面就會變成沒有語境化的孤兒 。而且，由於用戶通常通過搜索或其他方式繞過較高級別的導航頁面到達頁面，因此有必要提供返回到這些更高級別的路徑。</p><p>Local Navigation</p><p>B5. Local links to related content are also very useful.<br>與相關內容的本地鏈接也非常有用。</p><p>Structure <em>Can</em> Help</p><p>B6. Most sites have miserable information architectures that mirror the way the company internally thinks about the content and not the way users think about the content.<br>大多數網站都有悲慘的信息架構 ，這些架構反映了公司內部對內容的看法，而不是用戶對內容的看法。（沒人願意看的網站導覽）</p><p>B7. Users are so impatient on the Web that they don’t take time to learn about any individual website and its structure — instead, they proceed to the next site.<br>用戶對Web非常沒耐心，以至於他們沒有時間了解任何單獨的網站及其結構 — 而是轉向下一個網站。</p><h4>Navigation 原則與 Coursera 網站的對應</h4><p>以下會針對 Coursera 主要頁面，列出頁面上的元素對應哪項設計原則，並作詳述。</p><p><strong>登入後的首頁</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T--KwlGeqRYE4WuLS7Ti0Q.png" /></figure><ul><li>首頁就是個人頁面</li></ul><p>A1：用戶首先關注的是內容是否符合需求，Coursera 主打教學課程，便在主頁面放置目前使用者正在進行的課程，符合使用者需求。</p><ul><li>沒有大幅主動推銷的訊息</li></ul><p>A5：使用者尋找想要的課程，會依靠上方搜索列，而非被動接受推銷。</p><p>A6：Coursera 盡可能減少廣告，要點選 “Recommendations“ 才能看到推薦的項目，如此可增進使用者體驗，不會讓使用者被廣告轟炸。</p><p>這邊值得一提的是，Coursera 沒廣告策略和 Jakob 提出的原則吻合，卻和 Udemy 的商業策略相反，並不能說 Udemy 設計不佳，而是情境取向不同。</p><p>Coursera 的情境讓使用者主動去搜索想要的課程，網站並不會去 push 使用者做決定；Udemy 則是給予沒有特定想法、只是想瀏覽課程 window shopping 的使用者一些多元的選擇，還可以看到現在熱門的課程項目。</p><p>由此推論 Jakob 的原則，比較適用在主動閱覽資訊項目的資訊網站設計，而非電商類型的網站。</p><ul><li>上方沒有導覽，只有右上角點開有功能列表</li></ul><p>A2：使用者關注的是頁面內容，不會第一時間研究課程網站架構那些。（我這種刻意要研究的除外）。極簡化導覽列可以避免使用者分心，直接聚焦在畫面主要內容，也就是課程列表上面。</p><p>B2：同上，畫面中可點選的連結就是右上方功能表，中間的上課中清單，還有左邊的課程相關導覽，減少使用者操作網站的壓力。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*CvB9W1h7EENhc3htJGL9Tw.png" /></figure><ul><li>課程右上方有三個點，原本我完全忽略掉這個設計</li></ul><p>A8：這個設計元素不算新，漢堡和三個點都有 ”更多功能” 的意思在裡面。但我在使用的時候，完全沒注意到有三個點可以點選，而且還是 “課程退訂/改期功能” 這種大功能。</p><p>我認為這是 UI 編排上的問題，在這個卡片清單，視覺焦點會在左方的課程內容，與右方的進入課程按鈕。如果有更多功能，其實擺在 “Go to Course” 按鈕右側也許會更明顯，而且空間也夠大。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fPf_lfhI_w-hGLAzXki9WQ.png" /></figure><ul><li>每個頁面下方都有完整的 Coursera 網站導航</li></ul><p>B6：這些連結們應該就是 Jakob 提到的 “這些架構反映了公司內部對內容的看法，而不是用戶對內容的看法” 。我想 Coursera 在極簡的導覽上，卻保留了完整的網站結構連結，不把他從畫面隱藏掉，是為了更完整的功能。</p><p>一般使用不需要捲到下方，只有操作遇到困難，或是對課程付費方式想要更深入的了解，才會去從網站導覽找尋答案。這應該是折衷的做法，與其讓大家找想要的連結在哪裡，還不如全部攤開一目了然。</p><p><strong>課程頁面</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZstRsxJGK14j47wMm6V9sQ.png" /></figure><ul><li>課程資訊上方有導航麵包屑，可知道課程屬於哪個分類<br>B4：Coursera 提供了很好的結構鏈接，同 Jakob 所說：“使用者常通過搜索或其他方式繞過較高級別的導航頁面到達頁面” 。我並不知道課程分類，而是直接透過 ”UX” 關鍵字到目標頁面，這時看到導覽列有個 “設計和產品” ，我可以點下這個上層連結來瀏覽更多相關課程。</li></ul><p>這有兩個好處，一個是瀏覽課程中，使用者能比較相同類型的課程，找到更合適的項目；一個是教育使用者知道這個分類層級，如此一來下次想找 UX 課程，可以往設計跟產品方面搜索，能找到更多結果。</p><p>小反思：有些課程若是跨領域、屬於兩種類別，則麵包屑就會難以呈現。須謹慎使用。</p><h4>結語</h4><p>這次研究網站，最大的收穫是去發掘一些習慣的網站元素，原來背後都有深意。而 Jakob 的文章末尾也讓我驚豔… 他提到</p><p>“And I predict that Internet Explorer version 8.0 will be the first good Web browser that actually helps users navigate.”</p><p>這篇文章，居然是 2000 年寫的 @@</p><p>我不太清楚那個年代，瀏覽器跟網站導覽之間的關係，但這作者研究的成果與整理出來的原則，竟在過了18年後的現在，依然經的起考驗，不禁敬佩萬分。跟大家分享自己的小心得，若有任何批評或討論歡迎留言！</p><h4>參考資料</h4><ul><li><a href="https://www.nngroup.com/articles/is-navigation-useful/">Is Navigation Useful?</a></li><li><a href="https://coursera.org/">Coursera | Online Courses &amp; Credentials From Top Educators. Join for Free</a></li><li><a href="https://www.udemy.com/">Udemy: Online Courses for Skills, Careers &amp; AI</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80bb2f671170" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/coursera-website-ux-case-study-navigation-80bb2f671170">Coursera website UX case study — Navigation</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJS observable V.S Mobx observable]]></title>
            <link>https://medium.com/uwillx/rxjs-observable-v-s-mobx-observable-ead274a7e154?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/ead274a7e154</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[mobx]]></category>
            <category><![CDATA[dataflow]]></category>
            <category><![CDATA[observables]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Fri, 30 Mar 2018 09:11:59 GMT</pubDate>
            <atom:updated>2018-03-30T12:53:31.238Z</atom:updated>
            <content:encoded><![CDATA[<h4>可觀測資料流的兩大工具PK</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*MVOzim4Is3A4wAIBH76RaQ.png" /></figure><p>這次不是寫 Angular 切版實戰，因為.…..內容實在太龐大了有點難寫（光想要寫個「 如何用 Angular 建立 high order component 」就讓人很頭大）。所以決定先來開小差，寫最近遇到如何用 observable 來處理 Data Flow 的問題。</p><p>然後，最近也開始寫 React component，結果發現超好寫（自爆），不禁覺得 Angular 真的要再多加油，如果世界觀再打平一點就好了QQ。</p><p>這篇文章建議先有 observable （一種處理非同步工作的工具）的概念再來閱讀，可先看以下文章，他的實例非常好懂：</p><ul><li><a href="https://ithelp.ithome.com.tw/articles/10187005?sc=iThomeR">30 天精通 RxJS (05)： 建立 Observable(一) - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天</a></li><li><a href="https://ithelp.ithome.com.tw/articles/10188677">30 天精通 RxJS(23): Subject, BehaviorSubject, ReplaySubject, AsyncSubject - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天</a></li></ul><p>以前在處理非同步的工作、資料傳遞（像是跟 server API 拿資料、等待結果回傳再進行下一步動作），常用的有 promise 和 async await，observable 跟他們最大的差異，就是能夠</p><ol><li>實現 functional，取得 response 後可直接進行資料 parsing ，直觀好懂</li><li>實現 change listener，一旦訂閱，即可監聽未來所有數值的改變</li></ol><p><strong>RxJS observable </strong>用起來像這樣：</p><pre>export class DataStore {<br>  public <strong>data</strong>: BehaviorSubject&lt;number&gt; = new BehaviorSubject(0);</pre><pre>  update(<strong>newValue</strong>: number) { <strong>data</strong>.next(<strong>newValue</strong>); }<br>}</pre><pre>export class ButtonComponent {<br>  public <strong>data</strong>: number;<br>  constructor(private <strong>dataStore</strong>: DataStore) {<br>    <strong>dataStore.data.</strong>subscribe(<strong>x</strong> =&gt; <strong>this.data</strong> = <strong>x</strong>);<br>  }</pre><pre>  onBtnClick() { <strong>this.dataStore</strong>.update(<strong>this.data</strong> + 1); }<br>}</pre><p>由以上可知，我們在 DataStore 先設定了一個可供訂閱的 dataSubject、初始值為 0。ButtonComponent 在 constructor運作後就會訂閱 dataSubject 的所有變動，一旦變動就把最新的數值存到 data 裡面， 當按鈕按下去，會去呼叫 dataStore.update() 讓 dataSubject 存放的數值加一，進而觸發 data 改變。</p><p>這裡要注意到一點是，dataStore.dataSubject 只要有任何更新，<strong>所有訂閱他的元件</strong>都會拿取到最新的數值，這就是 observable 強大的地方，將所有與 dataStore 相關連的元件的資料溝通綁定在一起，不會再有一個動作改 A 元件結果沒改到 B 元件的情況。</p><p><strong>Mobx observable</strong> 用起來像這樣：</p><pre>export class DataStore {<br>  @observable public <strong>data</strong>: number;</pre><pre>  @action<br>  update(<strong>newValue</strong>: number) { <strong>this.data</strong> = <strong>newValue</strong>; }<br>}</pre><pre>export class ButtonComponent {<br>  public <strong>data</strong>: number;</pre><pre>  constructor(private <strong>dataStore</strong>: DataStore) {<br>    observe(<strong>dataStore</strong>, &#39;data&#39;, <strong>x</strong> =&gt; <strong>this.data</strong> = <strong>x.newValue</strong>);<br>  }</pre><pre>  onBtnClick() { <strong>this.dataStore</strong>.update(this.data + 1); }<br>}</pre><p>看起來 Mobx 較為直觀好懂，因為他藉由 decorator @ 讓變數有了可被觀測的能力。這裡注意一下我沒有使用 observe(dataStore.data, ...) 因為 observe 只支援觀測物件！如果要觀測 primitive value 要使用 observable box ，文章之後也會提到。</p><p><a href="https://mobx.js.org/refguide/boxed.html">boxed values | MobX</a></p><p>但接下來，將會遇到 Mobx 的弱點：<br>我們設定一個情境，要觀測資料開始 loading 與 loading完成的過程，加入isDataLoading 標記著資料開始改變到改變結束的狀態。</p><p><strong>RxJS</strong>：</p><pre>export class DataStore {<br>  public <strong>dataSubject</strong>: BehaviorSubject&lt;number&gt; = new BehaviorSubject(0);</pre><pre>  update(<strong>newValue</strong>: number) { <strong>dataSubject</strong>.next(<strong>newValue</strong>); }<br>}</pre><pre>export class ButtonComponent {<br>  public <strong>data</strong>: number;<br>  public <strong>isDataLoading</strong>: boolean;</pre><pre>  constructor(private <strong>dataStore</strong>: DataStore) {<br>    <strong>dataStore.dataSubject.</strong>subscribe(<strong>x</strong> =&gt; {<br>      <strong>this.data</strong> = <strong>x</strong>;<strong><br>      this.isDataLoading</strong> = false;<strong><br>    </strong>});<br>  }</pre><pre>  onBtnClick() {<br>   <strong> this.isDataLoading</strong> = true;<strong><br>    this.dataStore</strong>.update(<strong>this.data</strong>);<br>  }<br>}</pre><p><strong>Mobx</strong>：</p><pre>export class DataStore {<br>  @observable public <strong>data</strong>:number;</pre><pre>  @action<br>  update(<strong>newValue</strong>: number) { <strong>this.data</strong> = <strong>newValue</strong>; }<br>}</pre><pre>export class ButtonComponent {<br>  public <strong>data</strong>: number;<br>  public <strong>isDataLoading</strong>: boolean;</pre><pre>constructor(private <strong>dataStore</strong>: DataStore) {<br>    observe(<strong>dataStore</strong>, &#39;data&#39;, <strong>x</strong> =&gt; {<br>      <strong>this.data</strong> = <strong>x.newValue</strong>;<strong><br>      this.isDataLoading</strong> = false;<strong><br>    </strong>});<br>  }</pre><pre>  onBtnClick() {<br>   <strong> this.isDataLoading</strong> = true;<strong><br>    this.dataStore</strong>.update(<strong>this.data</strong>);<br>  }<br>}</pre><p>這裡會有個情況導致錯誤，observe 只有在 dataStore.data 改變時才會觸發，當我們執行 this.dataStore.update(this.data)因為數值沒改變，observe 無法觸發，將導致 isDataLoading 一直為 true。</p><p>必須改寫成：</p><pre>export class DataStore {<br>  public get <strong>data</strong>(): number { return <strong>this.dataObserve</strong>.get(); }<br>  public set <strong>data</strong>(v: number) { <strong>this.dataObserve</strong>.set(v); }<br>  public <strong>dataObservable</strong>: IObservableValue&lt;number&gt; = observable.box(0);<br>  <br>  update(<strong>newValue</strong>: number) { <strong>this.data</strong> = <strong>newValue</strong>; }<br>}</pre><pre>export class ButtonComponent {<br>  public <strong>data</strong>: number;<br>  public <strong>isDataLoading</strong>: boolean;</pre><pre>constructor(private <strong>dataStore</strong>: DataStore) {<br>    observe(<strong>dataStore.dataObservable</strong>, <strong>x</strong> =&gt; {<br>      <strong>this.data</strong> = <strong>x.newValue</strong>;<strong><br>      this.isDataLoading</strong> = false;<strong><br>    </strong>});<br>  }</pre><pre>onBtnClick() {<br>   <strong> this.isDataLoading</strong> = true;<strong><br>    this.dataStore</strong>.update(<strong>this.data</strong>);<br>  }<br>}</pre><p>這裡可看見 dataObservable 是個可觀測的 box 物件，便能使用observe(dataStore.dataObservable, ...) 並且不論數值是否更新，dataObserve.set(v) 都會觸發 observe 變動。</p><p>綜合以上想做個結論，一般在處理單純的資料同步上，Mobx 提供相當簡易好用的 coding style，而若牽涉到動作開始與結束、並非單純只要觀測資料數值的情境下，RxJS 提供了較簡單的作法。可在不同的情境上，自由取用合適的工具。譬如 data cache，user 不需知道 data cache 何時開始與結束，就可優先考慮 Mobx ；而頁面資料 API，幾乎都要提示 user 並在載入前後顯示 loading 狀態，就可考慮 observable。</p><h4>參考資料</h4><ul><li><a href="https://mobx.js.org/">Introduction | MobX</a></li><li><a href="http://reactivex.io/rxjs/">RxJS API Document</a></li><li><a href="https://ithelp.ithome.com.tw/users/20103367/ironman/1199">30 天精通 RxJS 系列文章列表 - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ead274a7e154" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/rxjs-observable-v-s-mobx-observable-ead274a7e154">RxJS observable V.S Mobx observable</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Figure out and divide Angular module in project]]></title>
            <link>https://medium.com/uwillx/figure-out-and-divide-angular-module-in-project-2a242e86bd8e?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a242e86bd8e</guid>
            <category><![CDATA[modules]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Mon, 29 Jan 2018 08:59:39 GMT</pubDate>
            <atom:updated>2018-02-01T03:45:07.335Z</atom:updated>
            <content:encoded><![CDATA[<h4>Angular 切版實戰 part 2: 如何在 Angular 專案裡切模組</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iUXBGYGcWQ4g4X28xin0yQ.png" /></figure><p><strong>前情提要</strong> （連載的概念）<br>距離上次發文（切分 shared feature）已經過了半年，我們的產品持續長大，光 shared feature 底下已經擁有40多支的元件，到處 import 到處 export。於是上週開始解決技術債，整理、重新分類 module 和元件們。</p><p>在講 Angular module 的構造前，不得不說寫了幾個月 Angular 後，才發現 Angular 處境有點尷尬。雖然是模仿了 React 的 component 概念，但 React 目前生態更活躍、元件更好寫，framework 方面 AngularJS 社群比較成熟，套件多，反觀 Angular 東找西找資源真的不多 (´･ω･`)。但凡走過才有收穫，部門也有 React 和 Vue 的開發者，剛好可以互相學習，取其所長，棄其所短。</p><p>之後目標是想辦法在擁有 Angular 框架的穩定下，又能學習 React 元件相依性降到最低、資料達成單一來源，不要讓 js 資料指來指去最後變成<a href="https://medium.com/@p5d12000/97f8756197df">折騰人心肝的小妖精</a>。之前被 js 傳遞資料全部用 pointer 的特性嚇到，趕快用 immutable 壓壓驚，不過資料流還沒理出頭緒阿 _(:3」ㄥ)_</p><p><strong>Angular 成員</strong></p><p>首先介紹 Angular 常用的成員：module、component、directive、service。</p><ul><li>module：大致可視為需執行某特定任務的功能區塊</li><li>component：呈現在 html 中的元件</li><li>directive：可彈性綁在 Component 身上的功能需求</li><li>service：inject 後可執行的某功能需求</li></ul><p>app 以 app.module 為進入點開始運行之後，依照不同的頁面需求，可藉由切換 module 或是 import 不同的 module 來決定此頁面要載入多少資源來完成工作。module 層級最高，可載入 module 、定義 component、inject service 等等。以我們專案的 HomeModule（Home 畫面所需的模組）最初的版本為例：</p><pre>@NgModule({<br>  imports: [<br>    ROUTES,<br>    CommonModule,<br>  ],<br>  declarations: [<br>    HomeComponent,<br>    HeaderComponent,<br>    TooltipDirective, ...<br>  ],<br>  provider: [<br>    SwitchAuthService, ...<br>  ]<br>  exports: [<br>  ]<br>})<br>export class HomeModule {}</pre><p><strong>Module 參數</strong></p><p>參考 Angular 文件，各 module 設定參數可粗淺說明為：</p><ul><li>imports：此模組需要載入的模組資源（只有 module 能被載入）</li><li>declarations ：此模組使用到的 component、directive</li><li>providers：此模組使用到的 Service（<strong>子模組</strong>也一樣可使用，providers 作用域比較廣，不像 imports 的資源只有自己本身可使用。用起來感覺很像.…..新增一支 js 檔案？很神祕還不太清楚）</li><li>exports：此模組輸出的資源（意即：其他模組若載入此模組，能使用的所有資源），輸出的資源必須是 imports、declarations 裡面定義過的項目。</li></ul><p>一開始還對 module 沒什麼概念，這初版立刻受到挑戰，在 home 的子 module — DashboardModule （dashboard 頁面）試圖加入 TooltipDirective 的時候，得到了 Angular “<a href="https://github.com/angular/angular/issues/10646">xxx is part of the declarations of 2 modules</a>” 的回應。Angular 規定 一個 component 只能附屬在一個 module 底下。所以為了讓不同模組可以共用元件，我們在第二版中創了個 SharedModule，包山包海 import 一堆 module 再 export 一堆 module、component、directive，每次有新頁面要使用共用元件，就只能 import 一大包 SharedModule，造成龐大負擔。於是切分 Module 是一定要的了。</p><p><strong>切分 Module</strong></p><p>在第三版中，切出了 UISharedModule（放置所有 UI 相關功能的模組）：</p><pre>@NgModule({<br>  imports: [<br>    CommonModule,<br>  ],<br>  declarations: [<br>    TooltipDirective,<br>    PlaceholderComponent, ...<br>  ],<br>  provider: [<br>    dialogService,<br>  ]<br>  exports: [<br>    TooltipDirective,<br>    PlaceholderComponent, ...<br>  ]<br>})<br>export class UISharedModule {}</pre><p>還有 DataPipeModule、LayoutSharedModule 等等其他功能性的模組區塊，並整理出以下特性：</p><ol><li>component / directive / pipe 等元件只能定義在一個 module 底下，因此只要有通用的元件，就要分類到某個 module 麾下，才能被多個 module 給 import 使用</li><li>exports 僅含 declaration 裡面的元件，儘量不要 export module，避免 module相依性、減輕 module 重量</li><li>Module 分越細，每個新頁面負擔越小，不會單一 import 就很重，但需要import的項目數量也會更多，需自行拿捏</li></ol><p>其中第二點提到的 &quot;不要 export module&quot; 是因為避免在 import module 的時候，載入了非預期的 module。譬如之前因為 TagModule 與 TagSharedModule 常常被一起使用，初版長這樣：</p><pre>@NgModule({<br>  imports: [<br>    TagSharedModule,<br>  ],<br>  declarations: [<br>    TagComponent,<br>  ],<br>  exports: [<br>    TagComponent,<br>    TagSharedModule, ...<br>  ]<br>})<br>export class TagModule {}</pre><p>原本是方便載入 TagModule 時可以連 TagSharedModule 一起使用。但也會導致原本只想使用 TagComponent 的 module 被迫載入 TagSharedModule。所以即使兩者常常被一起使用，TagModule 也還是不要 exports TagSharedModule 才好，需要兩者的時候再分別 imports：</p><pre>@NgModule({<br>  ...<br>  imports: [<br>    TagModule,<br>    TagSharedModule, ...<br>  ]<br>})<br>export class SomeModule {}</pre><p>原本只是想寫一下上週辛苦的切分 module 甘苦談，結果居然變成很正經的分享了 (*´･д･)? 不得不說 Angaulr 這種分層架構，對一切講求扁平的 component 世代很不利，但對團隊開發與理解是有幫助的。只希望之後優化專案的過程，能找到更好的結合兩者好處的開發方法。</p><p><strong>好文分享</strong></p><p><a href="https://risingstars.js.org/2017/zh/#section-framework">2017年 JavaScript 明星项目</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a242e86bd8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/figure-out-and-divide-angular-module-in-project-2a242e86bd8e">Figure out and divide Angular module in project</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 4 folders-by-feature structure practice]]></title>
            <link>https://medium.com/uwillx/angular-4-folders-by-feature-structure-practice-3ba6c366db62?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ba6c366db62</guid>
            <category><![CDATA[folder-structure]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Sun, 09 Jul 2017 07:20:21 GMT</pubDate>
            <atom:updated>2017-07-09T07:33:50.619Z</atom:updated>
            <content:encoded><![CDATA[<h4>Angular 4 切版實戰 part 1: 新手上路甘苦談</h4><p><em>註：未來 AngularJS (1.x) 後的版本統稱 Angular，不過為了方便理解暫且稱為 Angular 4，而且現在 Google &quot;Angular&quot; 的資訊也大多還是在講 AngualrJS</em></p><p>雖然字面上是甘苦談，但就是苦談的意思。之後可能會變成系列文，當作開發過程中的心得整理。</p><p>寫 AngularJS (以下簡稱 A1) 習慣之後，現在寫 Angular 4 (以下簡稱 A4) 有種打掉重練的感覺。尤其看著寫過 VueJS 的夥伴 <a href="https://medium.com/u/31815676b3d6">Eric Yip</a> 迅速寫完一堆 A4 的 component 和 service ，更加充滿徬徨。</p><p>寫過 Angular 1 的朋友們，一定懂 A1 directive 和 A4 Component 之間的大鴻溝，一開始只能稍微抓到 Component 可實現降低相依性、內部可獨立運作不影響外界邏輯。但真的實作起來，碰到了第一個難題：資料夾怎麼分？</p><p><a href="https://angular.io/guide/styleguide#folders-by-feature-structure">Angular Docs</a></p><p>文件告訴我們用 feature 來分。</p><p>feature 是甚麼……<br>shared 資料夾 ??? (腦中只有新垣結衣在吶喊シェア的畫面)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*2p1qzJ3s8za9KAkTMw1Qkw.jpeg" /></figure><p>明明當前端工程師兩年，此刻的我感到自己是如此的新鮮:D</p><p>於是大腦自行產生了<strong> 一切都是幻覺嚇不了我的 </strong>精神勝利法，第一版我們參考 <a href="https://github.com/AngularClass/angular-starter">AngularClass/angular-starter</a> 切出來長這樣：</p><p><em>註：由於是開發 CRM 系統，customer-list 代表顯示 user 的客戶列表。另外只列出少部分的資料夾當範例解說。</em></p><pre>app <br>--view<br>----home              // home page<br>----customer-list     // customer list page<br>----no-content        // 404 not found page<br>--component           // all the contents in page<br>----customer-profile  // single customer component<br>----header<br>----nav</pre><p>哇 component 和 view 平行了喔好棒棒~~~？</p><p>前輩：feature 完全沒分出來啊 = = 要用<strong>服務本身的特性</strong>來區隔，一個 feature 底下可以有很多 component 和 service，他們一起完成某樣<strong>同質性的任務</strong>。</p><p>我：可是假設現在有三個 feature，<br>1. profile feature: 專門產生個人資料頁面<br>2. business-card feature: 專門產生個人名片頁面<br>3. user feature: 專門處理個人資料相關的服務<br>而 1,2 的 feature 都要用到 3 提供的資料才能完成任務。這樣感覺 3 的地位和 1,2 不一樣，不該視為平行的 feature 阿？</p><p>前輩：那 user feature 就放到 shared 裡面阿！</p><p>我：！！！(⊙ｏ⊙)</p><p>前輩：然後<strong> feature 底下也可以有自己的 shared 資料夾</strong>，給 sub feature 使用，<strong>所有的 feature 都要盡可能平行，減少相依性</strong> (減少 parent-child 的關係)。</p><p>我：！！！(⊙ｏ⊙)</p><p>前輩一點醒，頓時如醍醐灌頂。原來 shared 的意思是，<strong>同樣層級的 feature </strong>可<strong>共享的內容</strong>！第二版打版成果：</p><pre>app<br>--core          // for one-time loading components/services<br>----header<br>----nav<br>--home<br>--customer<br>----customer-list      // customer list<br>----customer-profile   // single customer profile<br>----shared      // share with all thing under customer feature<br>--shared<br>----no-content</pre><p>使用 feature 所切出來的版，可以整理為幾項重點：</p><ol><li>使用 feature 來管理<strong>權限</strong>和<strong>作用的區域</strong>：一般預設 feature 要盡可能平行，當某 feature 底下需細分管理，則代表限制了它的作用不會影響到父層。<br>譬如 customer-list 不可 import home component，但 home可以 import customer-list。</li><li>app/customer/shared 裡面的內容只能在 customer 底下被使用，若在未來 home 也需要用到某支 app/customer/shared 的內容，則他應該要搬家搬到 更上層的 app/shared 裡面。</li><li>目前這一點還未實作，不過我推測未來若 customer-list 畫面需要和 customer-profile 同步，customer-list 不可直接 ngRepeat customer-profile.component，而是要在 app/customer/shared 裡面添增一筆單筆客戶資料的 component，由 customer-list 和 customer-profile 各自去 import 來用。因為萬一哪天customer-profile 頁面改了，才不會影響到 customer-list 顯示的結果。</li></ol><p>雖然才剛開始寫 component，有點懵懂抽象的概念，但實作成功就可以免除了每次維護都要尋找哪些元件有相依性、改一個元件結果頁面都要一起修正的地獄。現在新生之犢不畏虎，希望在 Angular 的世界可以殺出一條血路！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ba6c366db62" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/angular-4-folders-by-feature-structure-practice-3ba6c366db62">Angular 4 folders-by-feature structure practice</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular vs React — Stable Development & Innovative Technology]]></title>
            <link>https://medium.com/uwillx/angular-vs-react-stable-development-innovative-technology-d5744cdf80b2?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5744cdf80b2</guid>
            <category><![CDATA[comparison]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Mon, 19 Jun 2017 10:35:08 GMT</pubDate>
            <atom:updated>2017-07-09T05:01:39.743Z</atom:updated>
            <content:encoded><![CDATA[<h4>穩定發展與創新技術的抉擇</h4><p>這篇文章寫於去年 2016 年底，投稿於公司的技術論壇。最近開始寫 Angular 4（可是.….. Angular 5 也快要出來了orz），又把它翻出來，希望可以跟大家分享、討論。</p><p>在現今網路發達的時代，人們的生活已經離不開手機、電腦等可以聯網的裝置。不論是取得日常所需的資訊、下載休閒娛樂的遊戲，都能藉由網路來完成。隨著開發網頁和手機應用程式的需求不斷增加，前端工程師所面對的困難也變得更複雜，網頁應用程式需要跨平台、跨瀏覽器運作，並有響應式設計 (Responsive Web Design)。為了因應前端開發的各種需求，前端技術這幾年不斷地演進，開源社群中有越來越多新技術、網頁開發框與套件如雨後春筍般冒出，本文針對辯論已久的 Angular 與 React 做比較，並且從多個面相切入思考開發專案時該考慮哪些因素，如何選擇符合開發需求的前端技術。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*2I3Mtvj3UXIh8cOlwcLXzw.jpeg" /></figure><p><em>Photo by ACADGILD (</em><a href="https://acadgild.com/blog/angularjs-2-0-vs-reactjs/"><em>https://acadgild.com/blog/angularjs-2-0-vs-reactjs/</em></a><em>)</em></p><h3>Angular 和 React 的介紹與比較</h3><p>Angular 是由 Google 所開發的前端 JavaScript 框架，可以建立聲明式模板 (Declarative templates) 與實現資料繫結 (data-binding) ，讓開發者能夠將介面的運作邏輯寫入 HTML，控制介面的運作方式。Angular 提供一整套完整的專案環境與工具，讓開發者能快速上手，也可以實現 Single Page Application。2014 Google 並推出 Angular 2，針對網頁元件模組化加強處理，並提升效能。</p><p>React 是由 Facebook 所開發的 Javascript 程式庫，使用 JSX（一種使用 Javascript 來撰寫網頁元件的語法）建立 HTML 模板，強調模組化、將網頁劃分為獨立的區塊，並同樣能實現資料繫結。但 React 並不提供所有架構專案需要的工具，需使用第三方套件才能建立功能完整的網頁。也因此使用 React 讓開發者能更有彈性、更自由地選擇自己想要的套件，依照不同需求打造專案環境。</p><p><strong>Angular 的優勢</strong></p><p>Angular 和 React，一個是框架，一個是程式庫。框架能夠提供比程式庫更加完整的功能與環境，是 Angular 支持者極為推崇的優點。</p><p>Biznas(2016) 在解釋為何選擇了 Angular 2 作為開發企業用軟體的首選時提到，如果選擇了 React 程式庫，開發者還需要另外決定 routing、moodel management、build tools 要使用哪些第三方套件來實現。相較之下，如果選擇 Angular 框架，就不需另外尋尋覓覓，照著 Angular 的規範就能夠架起一個專案。</p><p>Cory House(2016) 也提到，Angular 提供更多建議給開發者，這種強制的一致性能幫助開發者更快適應其開發模式，也能讓開發者更能夠在不同開發團隊間迅速切換。</p><p>文章並用了具體的比喻來形容兩者的差異：「選擇 Angular 還是 React 就像選擇直接購買電腦還是買零件自己組裝一樣。」</p><p><strong>React 的優勢</strong></p><p>Angular 是 HTML 本位，把 Javascript 邏輯放到 HTML 裡面，而 React 則是 Javascript 本位，把 HTML 模板放到 Javascript 裡面。這使得兩種語言從最根本的寫作方式和邏輯上便有極大差異。支持 React 的開發者，推崇 React 使用較純粹的 Javascript 來開發網站，不用像 Angular 需要使用許多自創的功能性 attibutes（如：ng-repeat，ng-if) 來撰寫 HTML。</p><p>Cory House(2016) 的文章認為，JavaScript 的功能比 HTML 強大，因此增強 JavaScript 支援 HTML 刻模板的能力，要比增強 HTML 的邏輯能力還要合理。他也表示，很多應用不會用到 Angular 這種大型框架提供所有功能，現在時代越來越走向微服務、微應用、模組化的時代，React 能給予開發者挑選需要的模組的自由，讓應用程式能量身訂做。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ThJi-IGg2saUx8JawYhKgw.jpeg" /></figure><p><em>Photo credit: jwcarrol (</em><a href="https://twitter.com/jwcarroll"><em>https://twitter.com/jwcarroll</em></a><em>)</em></p><h3>開發企業用軟體的考量</h3><p>Angular 和 React 各有優缺點與支持的社群，那我們公司在前端開發上要如何選擇呢？</p><p>Biznas(2016) 提到，若企業想長久使用同一個技術來開發專案，有四大考量：技術一致性、社群的支援、最佳實作方法、未知數最小化。</p><ol><li>技術一致性</li><li>開發企業用軟體需要擁有增加新功能的彈性、能穩定的運作，以及能久遠的維護。技術有一致性，才能在修改舊專案時，不會影響到舊有的功能，保持專案的環境穩定，並且也能減少學習新技術的成本與使用新技術的風險。</li><li>社群的支援</li><li>有開發者眾多的開源社群，當開發遇到困難時能比較容易找到解答，也會有更多相關的套件可使用。</li><li>最佳實作方法</li><li>提供可依循的規則與實作指南，程式風格規格化與標準化，讓開發者能快速上手、並能在不同專案開發中更順利的切換，減少技術銜接的困難。</li><li>未知數最小化</li><li>提供的文件資訊與解決方案要詳細、完成度高，盡可能減少未知數，減少日後維護與開發的困難。</li></ol><p>若以這些因素來比較 Angular 和 React 的表現：</p><p>在技術一致性上，React 還在蓬勃發展中，隨時在推陳出新，要跟上它的腳步有一定困難度；而 Angular 則較穩定發展，在更新的同時也保持一定相容性，Angular 2 增加更多功能，舊專案可以部分採用新技術，慢慢把新支援的內容加入而不會影響原本的功能。</p><p>在社群的支援與最佳的實作方法方面，React 和 Angular 都有龐大開發者社群，但 React 較少標準化，不同開發者有不同的寫作風格，開發者需要花時間自行摸索寫作風格與開發要用的工具；而 Angular 提供較多建議與工具，較容易上手，開發者只要跟隨官方提供的最佳做法即可架構一個專案。</p><p>在未知數最小化方面，React 是成長中的社群，不同套件與工具彼此依賴度高，且 Facebook 並不會維護這些開發者所需要的第三方套件，這對企業軟體需要的穩定發展較不利；Angular 則能提供整體的開發環境，較少依賴非 Google 團隊開發的第三方套件。</p><h3>結論與未來發展</h3><p>React 是概念上的革新，掀起前端社群學習新技術的浪潮，模組化的精神改變了舊有網頁開發的思維，符合未來開發軟體應用的發展方向。Angular 相對穩定、不自由，但提供給企業軟體更可靠、更容易開發與維護的選擇。React 需要依賴相關套件來架構完整的專案，所以各個專案自由度大，專案間的差異性也可能較大；Angular 的專案則共通語言較多，對於開發企業用軟體產品的開發者，較容易進行後續技術維護、且有未來整合多個產品專案的可能性。</p><p>若說 React 是前端社群的啟發者，讓大家能自由揮灑創意，那麼 Angular 就像是領導者，Google 提供自身團隊研發的成果，讓開發者能跟隨、減少摸索的時間。若是開發新創應用程式，也許可考慮以 React 來架構客製化產品，走在浪潮的尖端；若是開發企業用軟體，則可選用 Angular ，穩定性高、整合也更容易！</p><h3>參考資料</h3><ul><li>Biznas(2016)：Why we chose Angular 2 over React for our enterprise software development work。Biznas Blog。取自 <a href="https://blog.biznas.io/392e2c9e39a9">https://blog.biznas.io/392e2c9e39a9</a></li><li>Cory House(2016)：Angular 2 versus React: There Will Be Blood。freeCodeCamp Blog。取自 <a href="https://medium.freecodecamp.com/66595faafd51">https://medium.freecodecamp.com/66595faafd51</a></li><li>程劭非(2016)：前端工程体系的演进。Modern Web 2016 Conference。取自 <a href="http://s.itho.me/modernweb/2016/keynote/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E4%BD%93%E7%B3%BB%E7%9A%84%E6%BC%94%E8%BF%9B_%E7%A8%8B%E5%8A%AD%E9%9D%9E.pdf">http://s.itho.me/modernweb/2016/keynote/前端工程体系的演进_程劭非.pdf</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5744cdf80b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/angular-vs-react-stable-development-innovative-technology-d5744cdf80b2">Angular vs React — Stable Development &amp; Innovative Technology</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JQuery Tutorial for Beginners]]></title>
            <link>https://medium.com/uwillx/jquery-beginner-tutorial-15a0f05c95f8?source=rss-4ffd4a89a178------2</link>
            <guid isPermaLink="false">https://medium.com/p/15a0f05c95f8</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Laura Lee]]></dc:creator>
            <pubDate>Sat, 03 Sep 2016 19:23:40 GMT</pubDate>
            <atom:updated>2016-09-06T01:25:58.770Z</atom:updated>
            <content:encoded><![CDATA[<h4>簡單上手JQuery — 給第一次寫程式的初學者</h4><p>由於身邊有越來越多設計師跨足前端網頁設計的朋友、同事，決定寫一篇文章來介紹 JQuery 語法，讓沒碰過程式的初學者也能輕鬆上手！文章會先簡單介紹 JQeury 的功用，舉一些例子來 demo 程式碼實作，最後提供幾個寫好程式的小方法。</p><h3>What is JQuery?</h3><p>JQuery 是 Javascript 的 libarary，可以讓 js 變得更直覺，更好寫。簡單來說，他可以讓工程師很方便的控制網頁裡面的元件，做出特定的動作，如：點擊按鈕後跳出視窗。</p><p>要產生行為，主要有三個要素：</p><ul><li>selector<br>目標元件 (element) 的選擇器，如：$(‘.info-button’) 可以選擇 class 為 info-button 的 element，$(‘li a’) 則可以選擇清單裡面的連結，也可使用逗號隔開，一次選取多個項目：$(‘.info-button, .nav-button, tab-button’)。&#39;$&#39; 就是代表 JQuery 的識別記號，讓 JQuery 能解析指令。</li><li>event<br>觸發動作的時機，如：點擊(click)，捲動(scroll)，滑鼠移過(hover)。<br>常用的 events 列表：<br><a href="http://www.w3schools.com/jquery/jquery_events.asp">http://www.w3schools.com/jquery/jquery_events.asp</a></li></ul><pre><strong>$(&#39;.btn&#39;).click</strong>(function() {<br>  // do something<br>});</pre><ul><li>function<br>執行的動作內容，如：跳出視窗，捲到頁面最上方，下拉選單等等。就和數學的 f(x) = x+1 一樣，function 有個小括弧可以承接變數，大括弧則包含了要做的事情：</li></ul><pre><strong>function plusTwo(x)</strong> {<br>  return x + 2;<br>} <br>var result = plusTwo(4); // result -&gt; 6</pre><pre><strong>function setHeight($element, hei)</strong> {<br>  $element.height(hei); // 呼叫 Jquery 提供的 height() function, 來設定高度<br>  console.log(&#39;setHeight successs&#39;); // 輸出成功訊息<br>}<br>setHeight($(&#39;.btn&#39;), 20); // btn 會被設為高度 20px</pre><ul><li>click event 裡面的 function 不用命名，因為不會有其他地方使用到他。</li><li>上面範例中，setHeight()的變數 $element, hei 可以自由命名，這裡 $element 命名故意加上錢字號 $ 是用來識別變數需要傳入 JQuery element $(‘…’)，才能正確運作。</li></ul><h4>Code Demo</h4><p>以下用五種 Code 寫法，實作”<strong>點擊按鈕後展開選單</strong>”的功能。雙左斜線//代表註解，在斜線後面的內容是不會被執行的，可以拿來寫筆記，或是暫時不執行該程式區塊。在底下 Demo 中，可以自行編輯 JS 區塊，取消不同 example() 的註解來切換要執行的 example code (不過結果其實相同)。大家可以比較看看<strong>同樣的功能，不同的寫法</strong>，可以造成如何的天壤之別：</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fl443018%2Fembed%2Fpreview%2FvXBaXv%3Fheight%3D600%26amp%3Bslug-hash%3DvXBaXv%26amp%3Bdefault-tabs%3Dhtml%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fl443018%2Fpen%2FvXBaXv&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/2141571101318e7141239cef9693adc5/href">https://medium.com/media/2141571101318e7141239cef9693adc5/href</a></iframe><p><strong>example1</strong></p><pre>function example1() {<br>  // 在點擊按鈕的時候，執行 function() :<br>  $(&#39;.btn&#39;).click(function() {<br>    // 切換選單狀態 (toggleClass), 若選單打開, 則關閉; 若選單關閉, 則打開<br>    $(&#39;.dropdown-menu&#39;).toggleClass(&#39;is-open&#39;);<br>  });<br>}</pre><p>toggleClass 可以切換選單開/合的狀態。這個寫法，只有<strong>點擊按鈕</strong>時能開合選單。但一般使用者的習慣，是希望點擊頁面的其他地方，選單也能合起來。這時可加入條件，在進行點擊時判斷<strong> event* </strong>的目標為何：</p><p><strong>example2</strong></p><pre>function example2() {<br>  // 在點擊頁面 (body) 的時候，執行 function()，並且用變數 e 來存放 event 內容<br>  $(&#39;body&#39;).click(function(e) {<br>    // 如果點擊的對象是按鈕, 則切換選單狀態<br>    if( $(&#39;.btn&#39;).is(e.target) ) {<br>      $(&#39;.dropdown-menu&#39;).toggleClass(&#39;is-open&#39;);<br>    } else {<br>      // 如果點擊的對象不是按鈕, 則關閉選單<br>      $(&#39;.dropdown-menu&#39;).removeClass(&#39;is-open&#39;);<br>    }<br>  });<br>}</pre><ul><li>event：當按鈕被點擊，JQuery 會回傳點擊事件 (click event) 的資訊，包含被點擊的項目 <strong>event.target</strong> 、點擊的位置 event.offsetX、event.offsetY 等等。每種 JQuery event 會回傳不同的資訊，可以用來做很多的用途。</li><li>上面範例中，在 body 頁面點擊到任何元件時，用了變數 e 來接收點擊事件的資訊，其中 $(‘.btn’).is(e.target) 可以判斷點擊目標是否是按鈕，</li></ul><p>常用的 JQuery css method：<br><a href="http://www.w3schools.com/jquery/jquery_ref_html.asp">http://www.w3schools.com/jquery/jquery_ref_html.asp</a></p><p>但這個範例在點擊空白頁面處、點擊選單時，選單都會收起來。我們希望點擊選單時，選單應該要保持打開，這樣該如何做呢？這是小考題，大家可以先想一想，答案在下方。</p><pre>function example3() { ...? }</pre><p>上面的 example3 大家是否有想出來呢？<br>這裡先給大家兩個複雜的範例 (這是真實案例，說出來都是淚……底下兩個範例還是簡化過的版本)：</p><p><strong>example3Complex</strong></p><pre>function example3Complex() {<br>  $(&#39;body&#39;).click(function(e) {</pre><pre>　　// 若點擊的地方不是 btn / dropdown-menu, 則關閉<br>    if ( !$(&#39;.btn, .dropdown-menu&#39;).is(e.target) ){<br>        $(&#39;.dropdown-menu&#39;).removeClass(&#39;is-open&#39;);<br>    }<br>    // 若點擊 btn / dropdown-menu, 分兩種情況<br>    else {<br>      // 如果選單打開, 而且點擊 btn, 則關閉<br>      if( $(&#39;.dropdown-menu&#39;).hasClass(&#39;is-open&#39;)<br>          &amp;&amp; $(&#39;.btn&#39;).is(e.target) ){<br>        $(&#39;.dropdown-menu&#39;).removeClass(&#39;is-open&#39;);<br>      } else {<br>        // (點擊 dropdown-menu) 保持打開<br>        $(&#39;.dropdown-menu&#39;).addClass(&#39;is-open&#39;);<br>      }<br>    }</pre><pre>  });<br>}</pre><p>有沒有覺得眼花撩亂，好難懂阿阿阿！！！<br>經過編修之後</p><pre>function example3ComplexEdit() {<br>  $(&#39;body&#39;).click(function(e) {</pre><pre>    // 若選單不是打開, 而且點擊的地方在 btn, 則打開選單<br>    if( !$(&#39;.dropdown-menu&#39;).hasClass(&#39;is-open&#39;)<br>       &amp;&amp; $(&#39;.btn&#39;).is(e.target) ) {<br>      $(&#39;.dropdown-menu&#39;).addClass(&#39;is-open&#39;);<br>    } <br>    // 若選單打開, 當點擊的地方不是選單時, 則關閉選單<br>    else if ($(&#39;.dropdown-menu&#39;).hasClass(&#39;is-open&#39;)<br>             &amp;&amp; !$(&#39;.dropdown-menu&#39;).is(e.target)){<br>      $(&#39;.dropdown-menu&#39;).removeClass(&#39;is-open&#39;);<br>    }</pre><pre>  });<br>};</pre><p>唉，好了一點，但還是有點複雜QQ<br>當要做的行為越來越複雜，硬寫絕對不是好的作法。要怎麼聰明的寫 Code呢？</p><h3>Thinking UI</h3><p>規劃介面行為，從畫圖開始。可以把點擊畫面個區塊會發生的事件畫出來，再抽象化成一行行的 Code 指令，這樣比較容易想像、寫出簡單好讀的 Code。</p><h4><strong>Draw all states</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*fkKAVKwBekh6Jd9yVzrlVA.png" /></figure><h4>List all states</h4><ol><li>點擊按鈕，會 toggleClass</li><li>點擊選單，無作用</li><li>點擊按鈕選單之外，選單會收起</li></ol><p>可以寫成</p><ol><li>if (是按鈕) { 則 toggleClass }</li><li>else if (是選單) { 沒有反應就只是選單 }</li><li>else { 不是按鈕也不是選單，那就收起選單 }</li></ol><p>Do Re Mi So ～～ 簡潔的程式碼就出爐了：</p><pre>function example3() {<br>  $(&#39;body&#39;).click(function (e) {<br>    <br>      if( $(&#39;.btn&#39;).is(e.target) ) {<br>        $(&#39;.dropdown-menu&#39;).toggleClass(&#39;is-open&#39;);<br>      } else if( $(&#39;.dropdown-menu&#39;).is(e.target) ) {<br>        // do nothing<br>      } else {<br>        $(&#39;.dropdown-menu&#39;).removeClass(&#39;is-open&#39;);<br>      }<br>    <br>  });<br>}</pre><p>可以和前面的範例比較，雖然執行的事情差不多，都是控制選單要開還是關，但最主要的差別是<strong>觸發事件的條件</strong>要怎麼寫才<strong>好讀、好懂，且邏輯不容易出錯</strong>。魔鬼總是常在細節裡，有時候條件沒訂好，常常會漏掉一些 special case，導致頁面元件沒有如預期般的運行。</p><h3>Simple JS Style Guide</h3><blockquote>“ 好的 Coding Style，讓工程師上天堂 ，下略 “</blockquote><p>能夠寫出基本的 JQuery 行為後，再來就是要寫好的 JS Style，好的可以讓 Code 更容易閱讀，讓下一個工程師更好維護！這部分不是針對 JQuery 作介紹，而是針對寫 Code 、使用 function 時要注意的通用事項。</p><h4>Variables</h4><p>盡量使用區域變數，定義全域變數很容易被誤改。</p><pre>// bad way<br>var counter = 0;<br>function plus1() {<br> counter = counter + 1;<br>}<br>function reset() {<br> counter = 0;<br>}<br>var count0 = counter, count2 = counter;<br>plus1();<br>reset(); // 一旦呼叫 reset(), 之前的 counter 就會不見了<br>count0 = counter; // reset 完, count0 == 0<br>plus1();<br>counter2 = counter; // 原本預期 plus1() 呼叫兩次要得到 counter2 == 2, 但中間被清空過所以 counter2 變成只剩 1</pre><pre>// good way<br>function plus1(recentCount) {<br>  return recentCount+ 1;<br>}<br>function reset() {<br>  return 0;<br>}</pre><pre>(function() { // 主程式區段<br>  var count0, count2; // 只有在主程式中可以修改這兩個變數<br>  count2 = plus1(count2);<br>  count0 = reset();<br>  count2 = plus1(count2);<br>});</pre><h4>Naming</h4><ul><li>小寫駝峰 helloWorld<br>一般 function / 變數命名都是小寫開頭，如：<br>var name / function initialize()<br>但如果單字有兩個以上，則第二個字首要大寫，就像駱駝的駝峰。<br>如：var firstName / function initializeBtn()<br>這裡需注意 function 命名，動詞擺前方，或是用觸發的時機命名<br>如：initializeBtn() / getData() / setStyle()<br>onWindowResize() / onMenuScroll()</li><li>大寫底線 HELLO_WORLD<br>只有定義”常數” (不可更動的數字)，才會用大寫底線。<br>如：MAX_WINDOW_WIDTH = 480;</li><li>dash分隔 hello-world<br>通常只用於 class selector：btn-primary</li></ul><p>— <em>其他較少用的種類</em> —</p><ul><li>大寫駝峰 HelloWorld<br>可用於特殊物件定義 (JS 物件導向寫法)<br>var UserInfo = function() {...}</li><li>小寫底線 hello_word<br>可用於變數命名(較不常用)，或是特殊標記：var last_name / var _name_</li></ul><p>詳細的 JS Style Document ：<a href="http://usejsdoc.org/">http://usejsdoc.org/</a></p><h4>Function Structure</h4><p>js 文件中，常看到很多直接把 code 塞在同一個 function ，這樣對 Coding 和維護專案的工程師，都是很不健康的一件事。</p><p>為了讓 Coding 整潔好讀，盡量把性質相同的程式碼擺在一起，需要找錯誤或修改內容時，就能快速找到需要修正的段落，而不會在茫茫程式海中一行行尋找。</p><pre>// bad way<br>$(document).ready(function(){<br>  $(&#39;.btn&#39;).click(...);<br>  $(&#39;.nav&#39;).click(...);<br>  $(&#39;body&#39;).scroll(...);<br>  $(window).resize(function(<br>    ...<br>  ));<br>});</pre><pre>// good way<br>function initializePage() {<br>  $(&#39;.btn&#39;).click(...);<br>  $(&#39;.nav&#39;).click(...);<br>  $(&#39;body&#39;).scroll(function() {<br>    onBodyScroll();<br>  });<br>}<br>function onBodyScroll() {<br>  // many things todo<br>}<br>function onWindowResize() {<br>  // many things todo<br>}</pre><pre>$(document).ready(function(){<br>  initializePage();<br>  $(window).resize(function(<br>    onWindowResize();<br>  ));<br>});</pre><p>這個例子中，把原本塞在 $(document).ready 裡面的內容都向外移動，只要有許多事情要做，就要獨立開一支 function 來執行，確保 function() 不會過於龐大。像是假設 body 捲動時要做很多事情，就不要全部塞入 initializePage()，而應該新開一支 onBodyScroll() 來控管，讓專案在便複雜之後， Coding 能保持好讀，不會太大包。</p><h3>結語</h3><p>JQuery 對新手入門是很棒的工具，相對好操作、好套用。雖然他也有許多缺點，像是結構鬆散，常常變成一堆散亂的元件，不好管理，但若想踏入工程師的世界，這應該是最入門款、最有親和力的。歡迎大家一起來寫 Code ！</p><blockquote>“ 只要有心，人人都可以是工程師 “</blockquote><p>碎碎念：原本只是想寫個開合選單的範例，到底怎麼可以變成這麼長的一篇文章，一邊寫一邊覺得這個好像要提一下，那個好像必須講，結果越寫越多。菜鳥工程師第一篇的技術文章，從基礎開始，希望大家會覺得有幫助 :)</p><h3>Q&amp;A</h3><ul><li>JS 有時候放在 head 前面，有時候放在 body 裡是為何？<br>&lt;head&gt; 是在頁面載入時一開始執行的區段，JS sources 可以放在裡面，但有時底下的 html 元件還沒載入時如果先執行 JS，就會來不及被 JS 初始化 (像是 JS 設定 btn 的點擊動作，萬一此時 btn 還沒出現，就會吃不到設定)，導致元件動作出錯。<br>一般較好的作法，&lt;head&gt; 裡面放 css sources ，&lt;body&gt; 最後面放 JS sources ，則執行順序：CSS載入-&gt; html 元件載入 -&gt; 最後才是 JS。<br>有客製化的 css / js 也要放在外掛下方，代表再另外附加上去的功能。</li></ul><pre>&lt;html&gt;</pre><pre>  &lt;head&gt;<br>    &lt;-- css 外掛們 --&gt;<br>    &lt;link href=&quot;bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br>    &lt;link href=&quot;vital-ui-kit.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br>    &lt;-- css 客製化 --&gt;<br>    &lt;link href=&quot;basic.css&quot; rel=&quot;stylesheet&quot;&gt;<br>  &lt;/head&gt;</pre><pre>  &lt;body&gt;<br>    &lt;-- 很多的頁面元件們 --&gt;<br>    &lt;div class=&quot;btn&quot;&gt;&lt;/div&gt;</pre><pre>    &lt;-- js 外掛們 --&gt;<br>    &lt;script type=&quot;text/javascript&quot; src=&quot;bootstrap.min.js&quot;&gt;&lt;/script&gt;<br>    &lt;script type=&quot;text/javascript&quot; src=&quot;vital-ui-kit.min.js&quot;&gt;&lt;/script&gt;<br>    &lt;-- js 客製化 --&gt;<br>    &lt;script type=&quot;text/javascript&quot; src=&quot;main.js&quot;&gt;&lt;/script&gt;<br>  &lt;/body&gt;</pre><pre>&lt;/html&gt;</pre><ul><li>kendo ui 的 jQuery 好像是 1.9.1，目前好像有更新的，若未來要換新的，會影響到其他的 JS 套件嗎？<br>專案的外掛有相依性，為了維持套件彼此之間不會衝突，我們不能更換到最新的版本，以免 kendo 會 GG。但如果未來 kendo 更新了，可以使用更高版本的 JQuery ，才能將版本升上去。<br>以目前的專案情況來說，JQuery 的功能已經夠用，所以不用擔心沒有升級會導致專案跑不動的情況～</li></ul><h3>Reference</h3><ul><li><a href="http://www.w3schools.com/jquery/jquery_events.asp">jQuery Event Methods</a></li><li><a href="http://www.w3schools.com/jquery/jquery_ref_html.asp">jQuery HTML / CSS Methods</a></li><li><a href="http://usejsdoc.org/">Use JSDoc: Index</a></li><li><a href="http://w3schools.com/">W3Schools Online Web Tutorials</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15a0f05c95f8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/uwillx/jquery-beginner-tutorial-15a0f05c95f8">JQuery Tutorial for Beginners</a> was originally published in <a href="https://medium.com/uwillx">uwillx</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>