<?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 Alex Yin on Medium]]></title>
        <description><![CDATA[Stories by Alex Yin on Medium]]></description>
        <link>https://medium.com/@alexyin0978?source=rss-138a55c85045------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*whnu9Gip62RXbU8UPpTxcQ.png</url>
            <title>Stories by Alex Yin on Medium</title>
            <link>https://medium.com/@alexyin0978?source=rss-138a55c85045------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:35:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexyin0978/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[Algorithms 1 — Big O Notation]]></title>
            <link>https://medium.com/@alexyin0978/algorithms-1-big-o-notation-4136eef5f8e6?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/4136eef5f8e6</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Tue, 15 Mar 2022 04:54:31 GMT</pubDate>
            <atom:updated>2022-03-15T04:54:31.173Z</atom:updated>
            <content:encoded><![CDATA[<h3>Algorithms 1 — Big O Notation</h3><p>部落格沉寂了一段時間，之前本來希望每周撥一些時間來解Codewars，後來索性直接在udemy上面找Algorithms and Data Structure的課程來上。而到目前為止也上了一段時間了，除了將time complexity與space complexity的基本觀念弄懂之外，也學會判斷object、array以及兩者相關的method的Big O是如何，最近則是剛上完common solving pattern，因此之後想要慢慢將筆記以及題目與解題方式放上來當作紀錄與複習，而因為是筆記性質，因此可能會有些片段與零碎。</p><h3>Big O</h3><p>O在這裡代表Operation，也就是執行一段程式會需要使用的步驟數，步驟會影響執行佔用的時間與記憶體，因此衍生出time complexity與space complexity。</p><p>所謂的time complexity即一段程式裡有多少operation，operation即為「加減乘除等於…」之類的運算符號，每多一個符號，電腦就必須多花一些時間去運算。</p><p>space complexity則為占用記憶體的operation，通常每宣告一個新的變數variable，就會增加占用的記憶體，也會增加space complexity。</p><h3>Counting Operation — time complexity</h3><p>在這裡我們舉一個範例題目，</p><pre>Write a function which takes an integer n, and will <br>give the sum of 1 to n</pre><p>我們先來看第一個解法:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbe1c3c04576df0fda6c22ec917db8e7/href">https://medium.com/media/dbe1c3c04576df0fda6c22ec917db8e7/href</a></iframe><p>在這裡我們可以看到，因為function內使用了for-loop，使我們的time complexity Big O變為O(n)，之所以會是O(n)的原因在於當我們今天輸入的n越來越大，在looping內會產生的operations就會越來越多，也就是裡頭計算的5n，因此我們就會說這種function的Big O是O(n)。</p><p>接著，我們來看另一個解法:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/078e1eaf79984fcfb7d7d457b05e00a9/href">https://medium.com/media/078e1eaf79984fcfb7d7d457b05e00a9/href</a></iframe><p>在這裡因為沒有使用到loop，因此會用到的operations數量是恆定的(也就是上面計算的3次)，不論n多大，所需的operations都是3次，因此我們會說這個function是O(1)，在執行時間的效率上大大優於第一個解法。</p><h3>各種不同的Big O</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kGGUjTQpo-wt1uyLMUJ3hw.png" /></figure><p>從剛剛範例可以理解到，當今天input的n越來越大，兩個解答所產生operation就會越差越大，我們用O(n)與O(1)分別代表這兩個解答的time complexity，解答一會因為輸入的n越來越大而造成operation越來越多的情況，而解答2則是恆定的operations。</p><p>除了O(n)與O(1之外，常見的Big O還有兩個，分別是O(n²)與O(log n)，這裡的log是以2為底數。</p><p>我們舉兩個例子說明O(n²)與O(log n):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee9c310e3a1cd5f0fb443021b3c7acc9/href">https://medium.com/media/ee9c310e3a1cd5f0fb443021b3c7acc9/href</a></iframe><p>上面這個例子為O(n²)，因為function內擺了一個nested loop，當今天一個loop為O(n)時，若此時loop內又放了一個loop，Big O就會變成n*n，也就是O(n²)，是時間效率蠻差的一個解法。</p><p>與之相反的是O(log n)，這裡直接以搜尋字典為例，當我們要從1本100頁的字典中去找到我們要的單字，與其一頁一頁搜尋(O(n))，我們可以直接用不斷對半搜尋的方法:「直接翻到第50頁，若所需要單字在50頁之後，則直接省略搜尋前50頁，翻到第75頁，若所需單字在75頁前，則捨去75頁之後的頁數，如此這般不斷對半搜尋，捨去另一半，直到找到答案」在這裡，我們將原本100頁的書(operations)不斷乘以1/2，因此Big O為O(log n)，效率僅次於O(1)。</p><h3>Space Complexity</h3><p>在前面有帶到，基本上space complexity即是要運作一段程式，裡面會占用到的記憶體，通常在宣告變數，儲存新的資料時，會占用新的記憶體空間，在Javascript中，所有資料可以分為兩種:primitives與objects</p><p>primitives包含boolean, number, undefined, null, string，除了string以外，前面四種primitives的space complexity為O(1)，而string因為每個字元就必須得算一個記憶體空間，因此當今天有n個字元就會佔去n個空間(arr.length)，因此為O(n)</p><p>objects就是除了primitives以外的資料皆可視為object，包含array, object, function等，跟string一樣，為O(n)，因為每個object與array內都可視為包含n個props(obj.length 或 arr.length)。</p><p>接著我們分別舉O(1)與O(n)的例子</p><h4>O(1)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6619d016c5dcaaf868ce4fb511bebbde/href">https://medium.com/media/6619d016c5dcaaf868ce4fb511bebbde/href</a></iframe><p>上面這個例子，因為在function內只有total與i這兩個新的變數被宣告，且皆非string或object類型，因此為O(1)</p><h4>O(n)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a78f53f7ea2e5464e5697c28bf7ddd87/href">https://medium.com/media/a78f53f7ea2e5464e5697c28bf7ddd87/href</a></iframe><p>因為我們將arr的props push到newArr中，換句話說，當今天arr有n個prop時，newArr內就會被儲存進n個prop，因此這個funciton為O(n)</p><h3>總結</h3><p>其實從上面space complexity的2個例子可以發現，空間複雜度與時間複雜度並沒有必然的關係，有時時間複雜度為O(n)，但是在空間複雜度卻是O(1)，因此並沒有絕對最好的解答，有的只有所謂的trade off，最佳解都取決於當下所需要的條件，要在時間複雜度上有效率，有時就會增加空間複雜度。</p><p>參考資料:</p><p>JavaScript Algorithms and Data Structures Masterclass - Colt Steele</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4136eef5f8e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codewars練習 - 6kyu -1]]></title>
            <link>https://medium.com/@alexyin0978/codewars%E7%B7%B4%E7%BF%92-6kyu-1-7bcc9dd61ae3?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bcc9dd61ae3</guid>
            <category><![CDATA[codewars]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Tue, 22 Feb 2022 04:36:36 GMT</pubDate>
            <atom:updated>2022-02-22T04:36:36.307Z</atom:updated>
            <content:encoded><![CDATA[<p>在上完Udemy的網頁課以後，由於深感自己對程式的基礎不足，空記住框架語法但基礎很差，長遠來講一定會需要花更多時間來惡補，因此希望透過做一些題目來練習Javascript的基礎，順便訓練google關鍵字技巧及邏輯能力。</p><h3>題目</h3><blockquote>The goal of this exercise is to convert a string to a new string where each character in the new string is “(“ if that character appears only once in the original string, or “)” if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.</blockquote><p>寫一個function，當string字串輸入此函數，若字串內字元只出現一次，則轉為’(’回傳，若字串內字元重複，則轉為’)’回傳。舉例：</p><pre>&quot;din&quot;      =&gt;  &quot;(((&quot; <br>&quot;recede&quot;   =&gt;  &quot;()()()&quot; <br>&quot;Success&quot;  =&gt;  &quot;)())())&quot; <br>&quot;(( @&quot;     =&gt;  &quot;))((&quot;</pre><h3>My Solution</h3><h4>First Step</h4><p>先將string轉為array，然後用array.map去將更改後的內容回傳到新的array，再將新的array轉回string，最後return新的string。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fe26db69a3d5143270daadef6a5fa1e/href">https://medium.com/media/2fe26db69a3d5143270daadef6a5fa1e/href</a></iframe><h4>Second Step</h4><p>在map裡，我們需要比對item與其他arr裡的prop是否相同（換句話說，字元是否重複），若相同，則回傳’)’，否則，回傳’(’。透過array.map，我們可以直接loop一遍arr裡所有item，然後將return之值複製到newArray內。</p><p>因此我們現在需要問的是，該如何在不重複與自己比較的情況下，比較item與其他prop?</p><p>要解決這個問題，我們需要在map裡面做一個for-loop，讓item可以跟arr內其他item去比較，在比較的同時，設定條件i !== index來控制不讓item與自身比較，以及item === arr[i]來找出是否item在arr內有重複。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7aedf672979337dbdc13e6ab28aa492/href">https://medium.com/media/d7aedf672979337dbdc13e6ab28aa492/href</a></iframe><h3>Others’ Solution</h3><p>提交後，因為可以順便去觀摩別人的解答，可以看到票選之最佳解（分成clever與best practice兩個投票項目）。我決定在這裡也放一下這題的best practice。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/874766b02d0a831fe197ef03c7a1cfab/href">https://medium.com/media/874766b02d0a831fe197ef03c7a1cfab/href</a></iframe><p>直接return word改造之後的結果，而在改造過程其實跟我的寫法一樣。</p><p>不同之處在於map內是使用arr.indexOf(item)去找出該item在arr裡的index number，與此同時，用arr.lastIndexOf(item)去找出arr裡最後一個指定item的位置，若兩者相同，代表沒有重複， return &#39;(&#39;；若不同，則代表有兩個以上重複item，return &#39;)&#39;這裡就可以用ternary operator去作出if-else的效果，更加簡潔。</p><h3>補充</h3><p>這裡大概整理一下在這個題目裡學習到的method</p><h4>1.string.split()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95011791e1feec17a53ff60b27fe6a28/href">https://medium.com/media/95011791e1feec17a53ff60b27fe6a28/href</a></iframe><h4>2.array.indexOf(item)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73153ffdbf8910d5af17216974cf20cc/href">https://medium.com/media/73153ffdbf8910d5af17216974cf20cc/href</a></iframe><h4>3.array.lastIndexOf(item)</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c76d8a418066bd2d3f1731b5345e3c6/href">https://medium.com/media/0c76d8a418066bd2d3f1731b5345e3c6/href</a></iframe><h4>4.array.join()</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8873a42f1594a712361824e69fab91cd/href">https://medium.com/media/8873a42f1594a712361824e69fab91cd/href</a></iframe><h3>後記</h3><p>後來才發現這題難度是6kyu，難怪卡這麼久（藉口，其實是因為平常缺乏邏輯訓練+Js method不熟），總之，第一次解題，解出來其實挺有成就感的!</p><p>但是因為最近也有開始學Algorithm，在知道了Big O的概念以後，發現自己的解答真是無比冗長阿，效能應該很差!</p><p>參考資料：</p><ol><li>[<a href="https://medium.com/@bebebobohaha/slice-splice-split-%E5%82%BB%E5%82%BB%E5%88%86%E4%B8%8D%E6%B8%85-46d9c8992729">JavaScript</a>] slice()、splice()、split() 傻傻分不清</li><li>String.prototype.<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase">toLowerCase()</a></li><li>JavaScript <a href="https://sebhastian.com/javascript-array-string/">array to string</a> (with and without commas)</li><li>Find <a href="https://flexiple.com/find-duplicates-javascript-array/">duplicates</a> in an array using javaScript</li><li>Array.prototype.<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf">indexOf()</a></li><li>Array.prototype.<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf">lastIndexOf()</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bcc9dd61ae3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modules]]></title>
            <link>https://medium.com/@alexyin0978/modules-c1b914cc167b?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1b914cc167b</guid>
            <category><![CDATA[modules]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Thu, 17 Feb 2022 04:47:19 GMT</pubDate>
            <atom:updated>2022-02-17T04:47:19.391Z</atom:updated>
            <content:encoded><![CDATA[<p>這一篇想要來講modules，以及modules的import與export，跟上兩篇<a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">What is URL?</a>和<a href="https://medium.com/@alexyin0978/absolute-path-relative-path-df9fe0fd8abc">Absolute Path &amp; Relative Path</a>算是同一個系列的。這裡整理了我個人對於module的一些觀念筆記以及疑問解答。</p><p>整篇文章會分成幾個重點：</p><ol><li>What is modules?</li><li>How to use modules?</li><li>Export &amp; Import</li></ol><h3>What is modules?</h3><blockquote>As our application grows bigger, we want to split it into multiple files, so called “modules”. A module is just a file. One script is one module.</blockquote><p>在寫project時（例如React app)，我們常會需要將檔案匯入另一個檔案，又或是匯入第三方API來作為資源使用，這種可供匯入，作為有效的外來資源來使用的js檔，就是所謂的module。一個module，可以視為一個script檔。</p><p>Module通常為兩種形式：</p><ol><li>Class</li><li>“library of functions” for a specific purpose.</li></ol><h3>How to use Modules?</h3><blockquote>“Modules” can load each other and use special directives “export” and “import” to interchange functionality.</blockquote><p>module的匯入與輸出是透過export &amp; import來達成，見以下範例：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edae553e35c09f236a0eb30b965ef9c8/href">https://medium.com/media/edae553e35c09f236a0eb30b965ef9c8/href</a></iframe><p>在script1.js裡，我們寫了一個函數func1，並export此函數，此時script1.js就可被視為一個module。而在export後，整個script1.js module以一個object的形式輸出，而func1則是裡面其中一個prop，因此我們可以在script2.js裡見到object destructure，如此一來才可使用func1。</p><h3>Export &amp; Import</h3><p>上述只簡單講解module的import與export，這裡，我們要細分三種export與import的形式與差異之處。</p><p>第一種， export {prop1, prop2} 與 import {prop1, prop2} from &#39;./file&#39;</p><p>直接將script1.js的內容export，並在script2.js直接將module destructure後import，此即為上面例子的方法。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2de8b428069a35492e1fdfe2828f4dd/href">https://medium.com/media/c2de8b428069a35492e1fdfe2828f4dd/href</a></iframe><p>第二種，import * as module_name from &#39;./file&#39;</p><p>今天在script1.js內有很多東西輸出，當在script2.js內要匯入時，若用object destructure一個一個將prop列出來太麻煩，因此可以直接將整個匯入之module命名，而其內容則用’.’去抓取。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b73031c1fdda1bee2983ffcdcb8b4f8d/href">https://medium.com/media/b73031c1fdda1bee2983ffcdcb8b4f8d/href</a></iframe><p>第三種，export default Module_name 與 import Module_name from &#39;./file&#39;</p><p>為react-app內的匯出與匯入形式，在export時若加上default，則代表此module內只有一個prop(通常為function或class)，這種方式常用在將此module作為一個component時會這樣使用。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86b8095cd3f3e0e6defddde2aab3b20e/href">https://medium.com/media/86b8095cd3f3e0e6defddde2aab3b20e/href</a></iframe><p>以上就是這次整理的module幾個可能以後會常看到的export與import形式。</p><p>參考資料：</p><ol><li>Modules, <a href="https://javascript.info/modules-intro#a-module-code-is-evaluated-only-the-first-time-when-imported">introduction</a></li><li>A module code is <a href="https://javascript.info/modules-intro#a-module-code-is-evaluated-only-the-first-time-when-imported">evaluated</a> only the first time when imported</li><li><a href="https://dwatow.github.io/2018/02-13-js-module-require-exports/">Node.js</a> 的 module.exports 和 require</li><li><a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/assign">Object</a>.assign()</li><li>Export <a href="https://javascript.info/import-export">and</a> Import</li><li><a href="https://stackoverflow.com/questions/61904877/why-js-file-extension-is-not-added-while-importing-a-component-in-reactjs">Why</a> .js [file Extension] is not added while importing a component in reactJS?</li><li>The <a href="https://stackoverflow.com/questions/46677752/the-difference-between-requirex-and-import-x">difference</a> between “require(x)” and “import x”</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1b914cc167b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Absolute Path & Relative Path]]></title>
            <link>https://medium.com/@alexyin0978/absolute-path-relative-path-df9fe0fd8abc?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/df9fe0fd8abc</guid>
            <category><![CDATA[url]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[path]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Sun, 13 Feb 2022 15:25:24 GMT</pubDate>
            <atom:updated>2022-02-13T15:25:24.595Z</atom:updated>
            <content:encoded><![CDATA[<p>繼上一篇<a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">What is URL?</a>整理了關於url的構造，這篇我想要來講講Absolute Path與Relative Path。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GUMfpgah9Ml4t6jn" /><figcaption>Photo by <a href="https://unsplash.com/@remotarjobs?utm_source=medium&amp;utm_medium=referral">Remotar Jobs</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What is Path?</h3><p>首先，這裡所指的Path(路徑)，即是我們在url裡所指的path to file</p><blockquote>A file path describes the location of a file in a web site’s folder structure.</blockquote><p>也就是說，在網頁結構底下，我要找到該網頁內的某一網頁、圖片、資料，由此root網頁一路尋找到該資料的路徑，即是path。</p><p>以上一篇文章<a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">What is URL?</a>的url為例:</p><pre><a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">https://medium.com/@alexyin0978/what-is-url-86f2233c096d</a></pre><p>這個url的root即為medium.com，而指向文章&quot;<a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">What is URL?</a>&quot;的path則是</p><pre>/@alexyin0978(作者帳號)/<a href="https://medium.com/@alexyin0978/what-is-url-86f2233c096d">what-is-url-86f2233c096d</a>(該文章)</pre><p>斜線/代表「前往」該檔案(資料夾)</p><p>而Path的形式分為兩種，第一種為Absolute Path，第二種為Relative Path</p><h3>Absolute Path</h3><blockquote>An absolute file path is the full URL to a file</blockquote><p>URL即是這種形式，會完整的將要到達該目標資料的路徑，從root一路寫出來，見此範例:</p><pre>root(domain)/path1/path2/file</pre><p>而有時，我們會省略root的名稱，直接以</p><pre>/path1/path2/file</pre><p>呈現，這種也是absolute path的一種。</p><h3>Relative Path</h3><blockquote>A relative file path points to a file relative to the current page.</blockquote><p>相對於absolute path將所有到達目標資料的路徑完完整整的交代清楚，relative path顯示的則是以「目前」位置到達該「資料」的路徑。這種相對路徑寫法，比較常用在從「檔案a」置入另一「檔案b」時。</p><p>舉例，今天有一資料夾src，裡頭有另外兩個資料夾folder1與folder2，在folder1裡面有script1.js與script2.js，在folder2裡面則有otherScript.js</p><pre>src/<br>  folder1/<br>    script1.js、script2.js<br>  folder2/<br>    otherScript.js</pre><p>現在我們來看看各種置入(import)檔案時，relative path的寫法:</p><p>假設我們開啟script1.js，並要在這檔案內另外import進script2.js與otherScript.js這兩個檔案：</p><pre>import script2 from &#39;./script2.js&#39;</pre><pre>import otherScript from &#39;../folder2/otherScript.js</pre><p>在這裡，一個點’.’代表的是目前所在位置；兩個點’..’代表回到上一層資料夾</p><p>今天若皆以absolute path的方式去寫路徑，則會長這個樣子：</p><pre>//If the root is: &quot;http://localhost:3000/&quot;</pre><pre>import script2 from &#39;/src/folder1/script2.js&#39;</pre><pre>import otherScript from &#39;/src/folder2/otherScript.js</pre><h4>那麼，該使用哪一種path寫法比較好?</h4><blockquote>It is best practice to use relative file paths (if possible). When using relative file paths, your web pages will not be bound to your current base URL. All links will work on your own computer (localhost) as well as on your current public domain and your future public domains.</blockquote><p>除了網頁url是用absolute path之外，一般在寫code時，要置入另一檔案，會建議使用relative path，如此才可以避免當檔案上傳到另一個電腦運作(heroku、github等),不會因為domain更換 造成底下檔案找不到path的情況。</p><p>參考資料:</p><ol><li>HTML File <a href="https://www.w3schools.com/html/html_filepaths.asp">Paths</a></li><li>Absolute vs. Relative <a href="https://medium.com/@colinlmcdonald/absolute-vs-relative-paths-7ffd8e31d49c">Paths</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df9fe0fd8abc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is URL?]]></title>
            <link>https://medium.com/@alexyin0978/what-is-url-86f2233c096d?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/86f2233c096d</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[url]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Thu, 10 Feb 2022 18:22:39 GMT</pubDate>
            <atom:updated>2022-02-10T18:22:39.850Z</atom:updated>
            <content:encoded><![CDATA[<p>因為習慣用Notion作筆記，最近開始計畫將過去上課的筆記和一些自己整理的資料寫到Medium裡。</p><p>回到今天的主題，What is <a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL">URL</a>?</p><blockquote><em>URL(Universal Resource Locator), it is used to identify a resource uniquely with the help of its location in a network of computers.</em></blockquote><p>簡單來說，就是我們俗稱的網址，也就是這個網頁在網際網路上獨特的位置編碼，我們可以透過輸入url，向指定server發送request，server在收到request後，會回傳（respond）資料(HTML, CSS, Javascript)給我們，瀏覽器(browser)會利用這些資料建構出我們需要的網站。</p><p>URL由幾個部分構成:</p><ul><li>Scheme 傳輸協定</li><li>Authority (Server位置，包含Domain Name 與Port)</li><li>Path to file(檔案、子網頁路徑）</li><li>Parameters（其他設定值，像是指定打開網頁後直接跳到某影片時間）</li><li>Anchor（直接跳到網頁某頁段）</li></ul><p>見此範例:</p><figure><img alt="https://cdn-images-1.medium.com/max/800/1*nHylTd9wjy7-lE9uTi0XlQ.png" src="https://cdn-images-1.medium.com/proxy/1*nHylTd9wjy7-lE9uTi0XlQ.png" /><figcaption>來源:<a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL">What is a URL?</a></figcaption></figure><h3>1. Scheme(Protocol) — 傳輸協定</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*ktX6XVfjwN3CqDKtyogbuA.png" /></figure><blockquote><em>The first part of the URL is the </em><strong>scheme</strong><em>, which indicates the protocol that the browser must use to request the resource (a protocol is a set method for exchanging or transferring data around a computer network).</em></blockquote><p>傳輸協定，規範網際網路間如何交換、傳送資料(request &amp; respond)，通常為http或https。</p><p>與Domain Name用 :// 分開。</p><h3>2. Authority — 網頁名稱</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*novvg3khXZ4xXwBmab6gCg.png" /></figure><p>包含了<strong>Domain Name </strong>以及<strong>Port</strong>兩者：</p><h4>a. Domain Name 網域名稱</h4><blockquote>The domain indicates which Web server is being requested. Usually this is a domain name, but an <a href="https://developer.mozilla.org/en-US/docs/Glossary/IP_Address">IP address</a> may also be used (but this is rare as it is much less convenient)</blockquote><p>Domain Name就是我要發送request的server的位置。</p><p>而除了使用Domain Name，也可以直接將其替換為IP位址(IP好比身分證字號，為一連串無意義的數字；而Domain Name則好比姓名，識別度較高）</p><p>Domain Name與Port之間以:隔開</p><h4>b. Port</h4><blockquote>The port indicates the technical “gate” used to access the resources on the web server. It is usually omitted if the web server uses the standard ports of the HTTP protocol (<strong>80 for HTTP</strong> and <strong>443 for HTTPS</strong>) to grant access to its resources. Otherwise it is mandatory.</blockquote><p>可以理解爲「通關密語」，而此密語則是依據傳輸協定(protocal)而定義(<strong>80 for HTTP</strong> and <strong>443 for HTTPS</strong>)，因此通常會省略（因為大家幾乎都一樣）。</p><h3>3. Path to file — 指定檔案路徑</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*qMKzTqrefRGlUHZ8xA10pQ.png" /></figure><blockquote>/path/to/myfile.html is the path to the resource on the Web server.</blockquote><p>若要得到server api內不同資料，可以透過/go/get/certain/file.html的方式去拿到api內不同資料，<strong>/</strong>代表前往的意思。</p><p>與Parameters之間以?隔開</p><h3><strong>4. Parameters — 設定值</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*FhIHyQO2l1fAofg8-FQdIg.png" /></figure><blockquote>?key1=value1&amp;key2=value2 are extra parameters provided to the Web server. Those parameters are a list of key/value pairs separated with the &amp; symbol. The Web server can use those parameters to do extra stuff before returning the resource. Each Web server has its own rules regarding parameters, and the only reliable way to know if a specific Web server is handling parameters is by asking the Web server owner.</blockquote><p>幾個重點：</p><ul><li>透過設定key=value來產生特殊功能</li><li>每個網頁的parameter設定都不太一樣，要知道這些設定的內容，只有像該網站主人詢問才有辦法知道</li><li>設定與設定之間以&amp;來隔開</li></ul><p>這樣可能還是很難懂，直接舉 Youtube影片當例子 —</p><blockquote><a href="https://www.youtube.com/watch?v=WzedDxXnPCs&amp;t=136s">https://www.youtube.com/watch?v=WzedDxXnPCs&amp;t=136s</a></blockquote><p>仔細看可以發現「?v=WzedDxXnPCs&amp;t=136s」就是youtube網站的parameters，而t=136s這個param可以讓使用者在打開這網頁時，直接跳到影片裡的這個時間。</p><h3><strong>5. Anchor</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*gI-SXnC8XpYlt9f9axjUQQ.png" /></figure><blockquote><em>#SomewhereInTheDocument</em> is an anchor to another part of the resource itself. An anchor represents a sort of &quot;bookmark&quot; inside the resource, giving the browser the directions to show the content located at that &quot;bookmarked&quot; spot. <strong>On an HTML document, for example, the browser will scroll to the point where the anchor is defined</strong>; on a video or audio document, the browser will try to go to the time the anchor represents. It is worth noting that the part after the <strong>#</strong>, also known as the <strong>fragment identifier</strong>, is never sent to the server with the request.</blockquote><p>終於來到最後一項，關於anchor的幾個重點</p><ul><li>前綴為#，其實就是代表html內的id</li><li>因此，在html內的id設定後，在url內設定#id，則會跳到該頁段</li><li>因為只是跳到同一頁面某頁段，因此並不會向server發出新request</li></ul><p>舉例說明，express官方的網站 —</p><blockquote><a href="http://expressjs.com/zh-tw/api.html#req.fresh">http://expressjs.com/zh-tw/api.html#req.fresh</a></blockquote><p>按下enter後，會跳到在html內id設定為req.fresh的頁段。</p><p>到這裡算是整理完url的資料了，終於完成我第一篇關於網頁的文章拉，太棒了！之後想要嘗試看看把在codewars的練習也寫成文章，不知道效果好不好。</p><p>參考資料:</p><p>1.What is a <a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL">URL</a>?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86f2233c096d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[致半年、一年、三年、五年後的自己]]></title>
            <link>https://medium.com/@alexyin0978/%E8%87%B4%E5%8D%8A%E5%B9%B4-%E4%B8%80%E5%B9%B4-%E4%B8%89%E5%B9%B4-%E4%BA%94%E5%B9%B4%E5%BE%8C%E7%9A%84%E8%87%AA%E5%B7%B1-7f04b26e942?source=rss-138a55c85045------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f04b26e942</guid>
            <category><![CDATA[成長]]></category>
            <category><![CDATA[motivation]]></category>
            <dc:creator><![CDATA[Alex Yin]]></dc:creator>
            <pubDate>Wed, 09 Feb 2022 18:16:00 GMT</pubDate>
            <atom:updated>2022-02-09T18:16:00.313Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>致半年、一年、三年、五年後的自己</blockquote><figure><img alt="Be Kobe" src="https://cdn-images-1.medium.com/max/1024/0*21qUpw8VxUyxFbKf" /><figcaption>Photo by <a href="https://unsplash.com/@tevic?utm_source=medium&amp;utm_medium=referral">Weichao Tang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>剛好逢巨人動畫第四季要完結，模仿一下《至兩千年後的你》這個標題，為我人生第一篇部落格文章添加一點點象徵意義。</p><p>這是第一篇部落格文章，並不打算長篇大論，只打算就最近的情況，去陳述最近的煩惱、心理狀態，還有給自己一些鼓勵的話。</p><p>從去年年底決心轉職開始，開始利用下班時間、週末時間，不管是去圖書館、咖啡廳，基本上除了睡覺以外，都待在外面用筆電上課、寫程式。可能有人會好奇，這樣不累嗎？累，但是同時，也覺得很亢奮，看到自己慢慢將整個網頁運作在腦中建構出來、慢慢搞懂前端在做什麼、後端怎麼respond、API是什麼、URL又是什麼，這些在我高中、大學從沒想過要去嘗試的，完全陌生領域的知識，一點點堆疊起來，最後有能力做出真實網頁產品，就很有成就感。</p><p>但這過程當中學到的最大收穫，並非學程式這件事情本身，而是面對新事物時的不確定性、面對迷惘、面對一開始這種「爛爛的」狀態所造成的挫折時，仍能厚著臉皮，咬牙忍住，不去逃避，不斷撐下去的這個「抗壓性」，以及「做，就對了」的執行力，這是到目前為止，整個學習過程帶給我最珍貴的兩個寶藏。</p><blockquote>“Don’t sell yourself short!”</blockquote><p>所以，我想告訴正在讀這篇文章的我自己，：「不要害怕做得很爛，因為凡事都要先做了，才能討論如何進步；不要害怕比別人差，不懂的東西，就多練習三遍、五遍、十遍，直到對這東西有信心。如果有什麼東西，是內心下意識想逃避，拖延而不去做的（就好比最近處理LinkedIn，練習Codewars，從頭自己寫project，太多讓我想拖延的事情了），那就去做，用力的做，唯有直球對決這些讓自己不舒服的事情，才能克服，才能讓自己從一開始不自在的狀態，慢慢磨練出實力與自信。所以，Don’t sell yourself short! 只有做了，才會知道原來自己可以這麼頑強，可以成長這麼快，然後不知不覺，變得這麼厲害！」</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f04b26e942" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>