<?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 PeiHsuan on Medium]]></title>
        <description><![CDATA[Stories by PeiHsuan on Medium]]></description>
        <link>https://medium.com/@peihsuan0416?source=rss-aad2b68e0521------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B2IPKarTzNroJbAPwcSv8w.png</url>
            <title>Stories by PeiHsuan on Medium</title>
            <link>https://medium.com/@peihsuan0416?source=rss-aad2b68e0521------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:35:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@peihsuan0416/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[【Ruby】小白學Rails：MVC是甚麼]]></title>
            <link>https://medium.com/@peihsuan0416/ruby-%E5%B0%8F%E7%99%BD%E5%AD%B8rails-mvc%E6%98%AF%E7%94%9A%E9%BA%BC-7976bb0e3c71?source=rss-aad2b68e0521------2</link>
            <guid isPermaLink="false">https://medium.com/p/7976bb0e3c71</guid>
            <category><![CDATA[model]]></category>
            <category><![CDATA[views]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[cruller]]></category>
            <dc:creator><![CDATA[PeiHsuan]]></dc:creator>
            <pubDate>Sun, 17 Apr 2022 06:22:45 GMT</pubDate>
            <atom:updated>2022-04-17T06:22:45.571Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>前情提要：此為初入新手村的超級菜鳥，探索JavaScript與Ruby on Rails的世界，僅供參考~ 敬請指教修正🙇🙇🙇</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ixh1j-ekzROadnxD1Lu5Sg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@joshuafuller?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Joshua Fuller</a> on <a href="https://unsplash.com/s/photos/ruby?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>在開始主題之前，先稍微認識一下Rails是甚麼吧~</p><blockquote>Ruby on Rails（官方簡稱為Rails，亦被簡稱為RoR），是一個使用Ruby語言寫的 開源Web應用框架，它是嚴格按照&lt;mark&gt;MVC結構&lt;/mark&gt;開發，努力使自身保持簡單，使實際應用開發時的代碼更少，使用最少的組態。 Rails 的設計原則包括「不做重複的事」（Don’t Repeat Yourself）和「慣例優於設定」（Convention Over Configuration）。－[Ruby on Rails (wiki)](<a href="https://zh.wikipedia.org/wiki/Ruby_on_Rails">https://zh.wikipedia.org/wiki/Ruby_on_Rails</a>)</blockquote><h4>Ruby on Rails</h4><ul><li>不是一種程式語言，是一種用Ruby做出來的框架/套件，是一種網站開發框架</li><li>發明人:David Heinemeier Hansson</li><li>2004年首次公開發表</li><li>別稱ROR</li></ul><p>framework 框架：制定一套規範或規則，大家在這規範下工作<br>網站開發框架=專門為網站開發所設計的工具包</p><h4>設計原則</h4><ul><li><strong>DRY</strong>不做重複的事Don’t Repeat Yourself<br> 透過物件導向的方式或是其他寫法讓東西可以重複使用</li><li><strong>COC</strong> 慣例優於設定 Convention Over Configuration<br> 如果可以順著Rails的慣例撰寫程式碼，會精簡很多<br> 順我者生，逆我者亡</li></ul><p>現在我們知道Rails的目的是讓開發者可以更容易進行網站開發，此外，透過建立MVC架構與原則，減少開發中需要互相串聯而耗費的力氣(例如:文件命名方式等)</p><p>那我們就進入主題，來看看甚麼是MVC結構吧~</p><h4>MVC結構</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yKyiRQk-_ktUQu3QFyep9g.png" /><figcaption>資料來源:五倍紅寶石-為你自己學 Ruby on Rails</figcaption></figure><p><strong>流程圖步驟說明:</strong></p><ol><li>當使用者輸入網址按下Enter之後會遇到<strong>Route</strong><br> Route像是<em>入口導覽員</em>的腳色，告訴對方下一步應該要怎麼做<br> ❓ Route阿桑是誰?</li><li>去找<strong>Controller</strong>，調相關資料</li><li>controller就像是到三樓一樣，會有很多櫃台<strong>action</strong><br> controller本身是個class<br> action就是方法method</li><li>到某一個櫃台(action)後，根據要的資訊，到相對應的<strong>Model</strong>調相關的資料出來。會根據需求看是否需要透過model調資料，可能需要可能不需要<br>❗注意!!model本身不是資料庫 ️</li><li><strong>Model會到資料庫調資料</strong><br> model像翻譯蒟蒻，但它不是資料庫，它用資料庫的查詢語言向資料庫調資料<br> 🔎 model和資料表(table)的關係?</li><li>資料庫找到的資料會再回傳給model</li><li><strong>model會再回傳給action</strong>，但回傳的資料本身是個roll data，是一般使用者看不懂的資料</li><li>action再將拿到的資料再去搭配畫面<strong>View</strong></li><li><strong>View將畫面組成一般人看得懂的資料後回傳回去</strong></li></ol><p>以上就是MVC架構的基本流程。</p><p>因為系統越做越複雜，所以將資料分類，Controller像是控制中心，將流程控制放於此，指揮Model和View。因此將資料邏輯全部放在model，流程控制放在controller，畫面相關放在View，便利於前後端分工。形成一套框架規範。</p><ul><li><strong>Model</strong>: 負責與資料庫溝通，處理資料庫相關的程式碼/資料邏輯</li><li><strong>Controller</strong>: 負責控制流程，處理資料流向</li><li><strong>View</strong>: 負責畫面的呈現，HTML放這，會回傳HTML的方法(method)</li></ul><blockquote><strong>Route</strong>是第一線處理request的人，很重要唷!!!也是讓Controller知道要處理哪個頁面。類似網址路徑，但它並不是網址（URL）本身，而是定義「網址」的「連線方式」以及「行為」的組合。之後我們會再談談R+MVC裡面的Route 門口阿桑是誰吧~</blockquote><p>MVC的特點就是「控制歸控制，資料歸資料，畫面歸畫面」。 基本的邏輯會是 「Controller 請 Model 處理資料，並設計資料流向，再讓 Views 被動接受資料並呈現畫面。」</p><h4>框架優點</h4><ol><li><strong>提供慣例</strong>，標準統一，較不會有銜接問題，方便管理，減少花在設定上的力氣，把更多精力放在開發本身。</li><li>擴充性高</li><li>使程式結構更直覺，開發快速</li><li>有利於團隊分工</li></ol><h4>框架缺點</h4><ol><li>要學新的語法</li><li>需要嚴謹的系統規劃</li><li>系統結構複雜，不適合小型專案</li><li>系統肥大、效能降低</li></ol><p>參考資料</p><ul><li><a href="https://railsbook.tw/chapters/10-mvc">Model、View、Controller 三分天下</a>(為你自己學 Ruby on Rails 高見龍)</li><li><a href="https://railsbook.tw/chapters/11-routes">Routes</a>(為你自己學 Ruby on Rails 高見龍)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7976bb0e3c71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[新手小白聊Function]]></title>
            <link>https://medium.com/@peihsuan0416/%E6%96%B0%E6%89%8B%E5%B0%8F%E7%99%BD%E8%81%8Afunction-98a69a3705d4?source=rss-aad2b68e0521------2</link>
            <guid isPermaLink="false">https://medium.com/p/98a69a3705d4</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[function]]></category>
            <dc:creator><![CDATA[PeiHsuan]]></dc:creator>
            <pubDate>Sat, 09 Apr 2022 03:24:36 GMT</pubDate>
            <atom:updated>2022-04-09T03:27:45.933Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>前情提要：此為初入新手村的超級菜鳥，淺談JavaScript裡的【Function】(淺談結果寫超長)，僅供參考~ 敬請指教修正 🙇 🙇 🙇</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*INfjPtQvb9etMGLVuS0YMw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@6heinz3r?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gabriel Heinzer</a> on <a href="https://unsplash.com/@6heinz3r?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3><strong>Function f(x)=3x+2</strong></h3><p>有沒有對於上方的公式看起來非常的熟悉，想到以前數學課一元一次方程式，二元一次方程式…好多好多的方程式，可怕的是滿天飛的xyz 😖</p><p>對於初入新手村的我而言，最最最最頭痛的莫過於：我甚麼時候要用function?甚麼時候要放參數?又甚麼時候會要return????</p><p>等等!參數是甚麼? return又是甚麼??? 😨哎呀呀~頭都要開始痛了</p><p>🔔<strong>function是在定義輸入值(參數)與輸出值(結果)之間的關係，並且給他一個名稱</strong></p><h3><strong>函數 Function</strong></h3><p>首先，我們先來看看以前熟悉的(或至少看過的)一元一次方程式，</p><p>以f(x)=3x+2為例：</p><p>🙎數學老師說：當你將<strong>未知數x</strong>等於1時，代入方程式裡面計算的方式會是像：f(1)=3x1+2這樣，x=1帶入，最後<strong>得到一個結果</strong>等於5。如果未知數x等於3時，代入則會得到11的答案，<strong>此未知數x也可以稱為變數</strong>，也就是會不斷改變的一個數字。</p><p>但有沒有發現，當方程式裡面的x代入各種數值的時候，這一個方程式f(x)=3x+2，就會給我一個答案，也因此它可以被<strong>重複使用</strong>。輸入一個值，給出一個值。就像你去便利商店買東西，給一塊錢得到一顆糖果 🍬</p><p>或許在此我們可以說</p><p>🔔 <strong>當你有需要重複使用的事情時，就可以使用function</strong></p><p>那這樣在javaScript裡會是甚麼樣的呢?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e58ca9d9302b2dc3b83beaa02cdaaac/href">https://medium.com/media/6e58ca9d9302b2dc3b83beaa02cdaaac/href</a></iframe><p><strong>函式結構：主要分為三個區域</strong></p><ol><li><strong>函式的名稱</strong>，如範例<em>*hello*</em>為function的命名</li><li>包圍在小括號()裡面的為function的<strong>參數</strong></li><li>包圍在大括號或稱花括號{}內的，為此function的<strong>作用區</strong></li></ol><p><strong>關於函式</strong></p><ul><li>函式是構成javascript的基本要素之一</li><li>啟動的時候要連同小括號一起，呼叫function</li><li>function是一種物件(object)也是一種可以被呼叫的值</li></ul><h3>👉function的命名</h3><p>在javaScript的結構上，可以幫function命名。能讓人較快速的知道此function是要進行什麼樣的事情。</p><p><strong>命名的方式可分為</strong></p><ul><li>具名函式 Named Function</li><li>匿名函式 Anonymous Function</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/202573a616bd15a801c9fcc41db37acc/href">https://medium.com/media/202573a616bd15a801c9fcc41db37acc/href</a></iframe><p>至於要用哪一個好呢??還是要看實際的狀況判斷，具名的可以叫他的名字出來~就能重複使用。</p><p><strong>小堤外：箭頭函式arrow</strong></p><ul><li>ES6之後出現的東西，比原本的函式有更簡短的寫法。</li><li>只能拿來取代匿名函式但在物件導向的時候，本質不太一樣。</li><li>屬於匿名函式的一種，不能命名</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d27d680dbc145771018326c569205d4/href">https://medium.com/media/3d27d680dbc145771018326c569205d4/href</a></iframe><p>🔎 後續延伸物件導向</p><h3>👉function的參數</h3><p>寫在小括號()內的屬於function的參數，就像前面提到的未知數x一樣，就屬於參數，也是function的輸入值。</p><p>有別於以往數學上的未知數常用的只有xyz，在JS裡面，function的參數可以是number、list等這些看起來像英文單字的參數，他其實就是一個被帶入的值，所以長甚麼樣子都可以，只要他在她的{}內有找到相對應使用的功能就可以。</p><ul><li>在function裡面的<strong>參數可以有很多個</strong>，並且用逗號區格</li><li>可以有<strong>預設值</strong>：讓function變得更有彈性，幫助流程控管，如果少帶引數給他的時候，就會出現預設值</li><li>可以用引數作為參數</li><li>也可以以物件作為參數</li></ul><p>🔎甚麼是物件作為參數?後續延伸那些可以做參數使用</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/018bce9d831af6f558a932905267bcda/href">https://medium.com/media/018bce9d831af6f558a932905267bcda/href</a></iframe><p>因此，同一個function被使用時，參數的數量不會不同，因為是<strong>被定義</strong>的，但引數的數量可能會不一樣(多給或少給)，就像你每天固定要買麥當當，但媽媽可能多給你10元或是少給你5元，所以你只得到大麥克單點或是麥香雞套餐。 😅可以這樣比喻嗎</p><p>❓<strong>甚麼時候要用參數呢</strong></p><p>當有引數需要被代入function時，就可以使用參數；就像是紙箱(function)一樣，把需要的工具(code block)放進去，需要用的時候打開紙箱(calling function)。</p><p>沒有參數的function就像是事件的集合體，直接進行作用區內的事情。</p><pre>💡 比較：<strong>引數(argument)與參數(paramter)</strong></pre><pre>● 引數是當我們呼叫函數時，傳遞給他的值 (媽媽給你錢)</pre><pre>● 參數是我們在函式定義中所列出的變數 (你要買的麥當當)</pre><h3>👉function的作用區<strong>{ }</strong></h3><p>在大括號或稱花括號內，套用function的參數。就像數學f(x)={3x+2}給他一個大括號，他就在大括號內計算，最終給你一個值(結果)。在function內定義的變數與外層是不同的世界。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/424a409149e44f75bf2b82d057bc3b06/href">https://medium.com/media/424a409149e44f75bf2b82d057bc3b06/href</a></iframe><h3>👉function的宣告</h3><p>直接用function時，呼叫hello()放哪裡都可以，但如果有兩個function時，後面定義的會把前面的覆蓋。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36defbffd97152dc5f6ffcfb993dc98c/href">https://medium.com/media/36defbffd97152dc5f6ffcfb993dc98c/href</a></iframe><pre>🙌 <strong>簡單小結論</strong></pre><pre><strong>- function是一個物件(object)也是一種值</strong></pre><pre><strong>- 可以被呼叫：透過名稱+小括號呼叫他</strong></pre><pre><strong>- 有屬於自己的作用區</strong></pre><pre><strong>- 可以帶入參數</strong></pre><pre><strong>- function也可以作為引數使用</strong></pre><pre><strong>- function裡面也可以包function</strong></pre><p>🔎 後續會再來談其他的宣告方式var/let/const，以及function的return示甚麼</p><p>😱😱😱新手小白努力了解學習~持續修正調整更新中😱😱😱</p><p><strong>參考資料</strong></p><ul><li>[<a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Guide/Functions">mozilla函式-JavaScript</a>]</li><li>[<a href="https://w3c.hexschool.com/blog/cb6e361">有點長的淺談 JavaScript function 函式</a>]</li><li>[<a href="https://ithelp.ithome.com.tw/articles/10204008">學JS的心路歷程 Day7-函式（二） arguments</a>]</li><li>[<a href="https://ithelp.ithome.com.tw/articles/10204008">隱含引數</a>]</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98a69a3705d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>