<?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 moonaarb on Medium]]></title>
        <description><![CDATA[Stories by moonaarb on Medium]]></description>
        <link>https://medium.com/@moonaarb7?source=rss-d091a3d24911------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*s_ZNLs7AMx3iBpIMKY54aA.jpeg</url>
            <title>Stories by moonaarb on Medium</title>
            <link>https://medium.com/@moonaarb7?source=rss-d091a3d24911------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 08:36:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@moonaarb7/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[Spread Operator “..” (2จุด) มีใน C# แล้วนะรู้ยัง ]]></title>
            <link>https://medium.com/@moonaarb7/spread-operator-2%E0%B8%88%E0%B8%B8%E0%B8%94-%E0%B8%A1%E0%B8%B5%E0%B9%83%E0%B8%99-c-%E0%B9%81%E0%B8%A5%E0%B9%89%E0%B8%A7%E0%B8%99%E0%B8%B0%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%A2%E0%B8%B1%E0%B8%87-06f5ae8aa10d?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/06f5ae8aa10d</guid>
            <category><![CDATA[csharp-programming]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[collection]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Sun, 28 Jan 2024 14:37:46 GMT</pubDate>
            <atom:updated>2024-04-25T15:50:53.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Spread Operator “..” (2จุด) มีใน C# แล้วนะรู้ยัง — Intro" src="https://cdn-images-1.medium.com/max/986/1*_-MDY90Mj_3fPJKT-VQszQ.png" /></figure><blockquote>เพื่อนๆ เคยใช้ “..” (2จุด) หรือที่เรียกว่า “Spread Operator” กันไหม ?</blockquote><p>ใน C# ก็มีให้ใช้ได้แล้วนะ ซึ่งเจ้าสิ่งนี้มันมีประโยชน์ม๊ากกกกกกกก 👏👏</p><p>ใช้งานยังไงมาดูกัน</p><figure><img alt="Spread Operator คืออะไร ?" src="https://cdn-images-1.medium.com/max/1024/1*pJyek99vC7jpNlFyKOyZlg.png" /></figure><h3><strong><em>Spread Operator คืออะไร ?</em></strong></h3><p>ถึงแม้ “Spread” จะแปลว่า การแยกออก คลี่ กระจาย… แต่ Spread Operator นี้ ไม่ได้เอามาใช้เพื่อจุดประสงค์แยกค่าออกจากกันแต่อย่างใด</p><blockquote>ใน C# 12 หรือ .NET 8 (Visual Studio 17.8.x ขึ้นไป) สามารถใช้ Spread Operator หรือเครื่องหมาย “..” (2จุด) นี้ เพื่อกรณีที่ต้องการรวมข้อมูลหลายๆ Collection เข้าด้วยกัน</blockquote><h3>วิธีใช้งาน</h3><ol><li>ใช้งานกับข้อมูล Collection เท่านั้น</li><li>ใส่เครื่องหมาย <em>“..”</em> (2จุด) ในโค้ด ตามด้วยข้อมูล Collection ตรงส่วนที่ต้องการแทรก Collection อื่นๆ เข้าไป</li><li>สามารถใช้กับ Collection ประเภทข้อมูลเดียวกัน หรือต่างกันได้ เช่น List&lt;int&gt; กับ int[] เป็นต้น</li><li>ระบุประเภทตัวแปรให้ชัดเจน</li></ol><figure><img alt="วิธีใช้งาน Spread Operator หรือเครื่องหมาย “..” (2จุด)" src="https://cdn-images-1.medium.com/max/1024/1*Qv3QxyeIzAG0kxZYS7kweA.png" /><figcaption>วิธีใช้งาน Spread Operator หรือเครื่องหมาย <em>“..”</em> (2จุด)</figcaption></figure><h3>ประเภทข้อมูลที่รองรับ</h3><ol><li>Array <em>(เช่น int[])</em></li><li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.span-1">System.Span&lt;T&gt;</a> และ <a href="https://learn.microsoft.com/en-us/dotnet/api/system.readonlyspan-1">System.ReadOnlySpan&lt;T&gt;</a> <em>(เช่น System.Span&lt;int&gt;)</em></li><li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1">List&lt;T&gt;</a> <em>(เช่น List&lt;int&gt;, IEnumerable&lt;int&gt;, ICollection&lt;int&gt;)</em></li></ol><blockquote>— int[] กับ int[] ✅</blockquote><blockquote>— List&lt;int&gt; กับ int[] ✅</blockquote><blockquote>— System.Span&lt;int&gt;<em> </em>กับ List&lt;T&gt; ✅</blockquote><blockquote>— int[] กับ System.Span&lt;int&gt; ✅</blockquote><blockquote>— int[], Span&lt;int&gt; และ List&lt;int&gt; ✅</blockquote><blockquote>— string[] กับ Span&lt;int&gt; ❌</blockquote><blockquote>สรุปคือ ข้อมูลใดๆ ก็ตาม ที่อยู่ในประเภทข้อมูลที่รองรับ ทั้งหมดนี้สามารถใช้ Spread Operator ร่วมกันได้นั่นเอง และในผลรวม Collection ตัวสุดท้าย จะเลือกประกาศประเภทข้อมูลอะไรก็ได้ตามที่มีในนี้ได้อีกด้วย</blockquote><h3>การทำงาน</h3><figure><img alt="การทำงาน Spread Operator หรือเครื่องหมาย “..” (2จุด)" src="https://cdn-images-1.medium.com/max/1024/1*bLLSYusZDy1ob4mqocjRJg.png" /><figcaption>การทำงาน Spread Operator หรือเครื่องหมาย <em>“..”</em> (2จุด)</figcaption></figure><blockquote>จากรูป [1, 2, ..array2] คือการเอาค่าในตัวแปร array2 มาต่อท้ายค่า 1, 2 ตามลำดับ index</blockquote><p>การทำงานของ Spread Operator หรือเครื่องหมาย <em>“..”</em> (2จุด) นี้ จะทำการกระจายข้อมูลสมาชิกใน Collection ออกมา และแทรกสมาชิกอื่นๆ เข้าไปตามลำดับ index ผลลัพธ์สุดท้ายจะได้ Collection ใหม่ ที่มีข้อมูลสมาชิกทั้งหมดรวมอยู่ด้วยกัน ซึ่งจัดเรียงถูกต้องตามลำดับ index ที่ใส่เข้ามานั่นเอง</p><h3>ตัวอย่างการใช้งาน</h3><ol><li>ใช้ Spread Operator กับ Collection ประเภทข้อมูลเดียวกัน</li></ol><blockquote>จากตัวอย่าง รวมข้อมูลประเภท int[] ทั้ง 3 ที่ ให้อยู่ใน Collection เดียวกัน</blockquote><pre>int[] array1 = [1, 2, 3];    <br>int[] array2 = [4, 5, 6];    <br>int[] array3 = [7, 8, 9];   <br><br>int[] concatArray = [..array1, ..array2, ..array3];    <br><br>// output:<br>// 1, 2, 3, 4, 5, 6, 7, 8, 9 </pre><p>สามารถแทรก Collection อื่นๆ ระหว่างทางได้ แบบนี้ 👇</p><pre>int[] array2 = [4, 5, 6];<br>int[] array3 = [10, 11];<br><br>int[] concatArray = [1, 2, 3, ..array2, 7, 8, 9, ..array3]; <br><br>// output:<br>// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11  </pre><p>2. ใช้ Spread Operator กับ Collection ประเภทข้อมูลต่างกัน</p><blockquote>จากตัวอย่าง รวมข้อมูลประเภท int[], List&lt;int&gt; และ Span&lt;int&gt;ให้อยู่ใน Collection ข้อมูลประเภท ICollection&lt;int&gt; เดียวกัน</blockquote><pre>int[] array1 = [1, 2, 3];<br>List&lt;int&gt; array2 = [4, 5, 6];<br>Span&lt;int&gt; array3 = [7, 8, 9];<br><br>ICollection&lt;int&gt; concatArray = [..array1, ..array2, ..array3];<br><br>// output:<br>// 1, 2, 3, 4, 5, 6, 7, 8, 9  </pre><h3>ประโยชน์ของ Spread Operator</h3><ul><li>สามารถรวม Collection ได้ง่าย โค้ดสั้น และเข้าใจง่าย</li></ul><h3>ข้อควรระวัง</h3><ol><li>ใช้ Spread Operator กับข้อมูล Collection เท่านั้น</li><li>สามารถใช้กับ Collection ประเภทข้อมูลเดียวกัน หรือต่างกันได้ ตามประเภทข้อมูลที่รองรับเท่านั้น</li><li>ต้องระบุประเภทตัวแปรให้ชัดเจน</li></ol><blockquote>var concatArray = [1, 2 , ..array2]; ❌</blockquote><blockquote>int[] concatArray = [1, 2 , ..array2]; ✅</blockquote><h3>สรุป</h3><p>ใน C# 12 หรือ .NET 8 (Visual Studio 17.8.x ขึ้นไป) สามารถใช้ Spread Operator “..” (2จุด) เพื่อรวม Collection ได้แบบง่ายๆ แถมโค้ดยังสั้นอีกด้วย</p><blockquote>…หวังว่าเพื่อนๆ จะลองเอาไปใช้งานกันดูนะ 👋</blockquote><p>แหล่งที่มา</p><p><a href="https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12">What&#39;s new in C# 12</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=06f5ae8aa10d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[yield return แบบนี้ก็ได้เหรอ?]]></title>
            <link>https://medium.com/@moonaarb7/yield-return-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B8%99%E0%B8%B5%E0%B9%89%E0%B8%81%E0%B9%87%E0%B9%84%E0%B8%94%E0%B9%89%E0%B9%80%E0%B8%AB%E0%B8%A3%E0%B8%AD-3b4d7836f1ac?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b4d7836f1ac</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Sat, 04 Nov 2023 16:58:21 GMT</pubDate>
            <atom:updated>2024-01-28T08:14:37.831Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="yield return แบบนี้ก็ได้เหรอ? — cover" src="https://cdn-images-1.medium.com/max/1024/1*AuRosK5NqiDS8lXkFVlgPw.png" /></figure><blockquote><em>เคยไหม? อยากจะเข้าถึงข้อมูล Collection อะไรสักอย่าง แต่ขี้เกียจประกาศตัวแปรมารับค่า </em>😰</blockquote><blockquote><em>ลองใช้ </em>yield return <em>ดูสิ…</em></blockquote><figure><img alt="Collection List รายชื่อ และอายุของสมาชิกวง Treasure" src="https://cdn-images-1.medium.com/max/1024/1*VxIc8fSJQpC1PhqdPV01eA.png" /><figcaption>Collection รายชื่อ และอายุของสมาชิกวง Treasure</figcaption></figure><p><strong>โจทย์</strong></p><p>จากข้อมูลข้างบน 👆 ต้องการใช้ <em>for loop</em> เพื่อดูรายชื่อสมาชิกวง Treasure ที่มีอายุตั้งแต่ 23 ปีขึ้นไป</p><blockquote>ผลลัพธ์ คือ Jihoon, Hyunsuk และ Yoshi</blockquote><h3><strong>มาดูวิธีทำกัน </strong>❗</h3><p><strong>วิธีที่ 1 ประกาศตัวแปรเพื่อเก็บค่า</strong></p><p>คือ ประกาศตัวแปร ประเภท List&lt;string&gt; เพื่อเอามารับค่าชั่วคราว แล้วสั่ง return ตัวแปรออกทีเดียว</p><p>ในตัวอย่างนี้ ประกาศตัวแปร memberResultList ประเภท List&lt;string&gt; ขึ้นมา เพื่อเก็บค่าเข้าตัวแปรแต่ละรอบไปเรื่อยๆ จนกว่าจะหมดรอบ loop การทำงาน จึง Return ตัวแปรนี้ออกไป</p><pre>static List&lt;string&gt; SearchAgeRange()<br>{<br>  List&lt;string&gt; memberResultList = new List&lt;string&gt;(); /* ประกาศตัวแปรเพื่อเก็บค่า */<br>  foreach (PersonModel personItem in memberList)<br>  {<br>       if (personItem.Age &gt;= 23)<br>           memberResultList.Add(personItem.Name);    /* เก็บค่าเข้าตัวแปร */<br>  }<br>  return memberResultList;                           /* Return ตัวแปรออกไป */<br>}</pre><figure><img alt="List รายชื่อ และอายุของสมาชิกวง Treasure" src="https://cdn-images-1.medium.com/max/742/1*vkFtqujzSv-HBHWb0oS0xA.png" /><figcaption>ผลลัพธ์ วิธีที่ 1 ประกาศตัวแปรเพื่อเก็บค่า</figcaption></figure><p>ซึ่งก็ได้ผลลัพธ์ที่ถูกต้อง <em>แต่!!!!!!!!</em></p><p>พลังแห่งความขี้เกียจชนะทุกสิ่ง มีอะไรที่สั้นกว่านี้มะ🤔</p><p><strong>วิธีที่ 2 ใช้ yield return</strong></p><pre>static IEnumerable&lt;string&gt; SearchAgeRange()<br>{<br>  foreach (PersonModel personItem in memberList)<br>  {<br>      if (personItem.Age &gt;= 23)<br>          yield return personItem.Name;    /* ใช้ yield return ผลลัพธ์ */<br>  }<br>}</pre><figure><img alt="ผลลัพธ์ วิธีที่ 2 ใช้ yield return" src="https://cdn-images-1.medium.com/max/737/1*lCH0DtxNrY_ULWRwpHA-Pw.png" /><figcaption>ผลลัพธ์ วิธีที่ 2 ใช้ yield return</figcaption></figure><blockquote>ผลลัพธ์ถูกต้อง โดยไม่ต้องมีอะไรมารับ👏👏</blockquote><h3>เปรียบเทียบโค้ด</h3><figure><img alt="เปรียบเทรียบ Before &amp; After การใช้ yield return" src="https://cdn-images-1.medium.com/max/1024/1*6PJ_CnNVLawdWHOsNP90dg.png" /><figcaption>เปรียบเทียบ Before &amp; After การใช้ yield return</figcaption></figure><blockquote>จะเห็นว่า โค้ดฝั่งที่ใช้ yield return สั้นกว่า เพราะมีบรรทัดเดียวแบบเก๋ๆ ต่อให้ข้อมูลเยอะ ก็ไม่ต้องสร้างตัวแปรมารับค่าให้ยุ่งยาก😍</blockquote><p><em>แต่ Data type ที่ได้จาก yield return ทำไมจึงได้ IEnumerable นะ?</em></p><blockquote>ก่อนอื่นเรามาทำความรู้จัก yield return กันก่อน</blockquote><h3>yield return คืออะไร ?</h3><blockquote>yield return เป็น method ที่เป็น <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable">IEnumerable</a> ใช้ในการ return ค่าแต่ละค่าออกมา ซึ่งค่าที่ return ออกมาสามารถมีได้มากกว่า 1 ค่า</blockquote><blockquote>ดังนั้น ผลลัพธ์ที่ได้จาก yield return คือ <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1">IEnumerable&lt;T&gt;</a> หรือ <a href="https://learn-microsoft-com.translate.goog/en-us/dotnet/api/system.collections.generic.iasyncenumerable-1?_x_tr_sl=en&amp;_x_tr_tl=th&amp;_x_tr_hl=th&amp;_x_tr_pto=wapp">IAsyncEnumerable&lt;T&gt;</a> นั่นเอง</blockquote><h3><strong>การทำงาน</strong></h3><figure><img alt="การทำงานของ yield return" src="https://cdn-images-1.medium.com/max/1024/1*AJ9Mtk6vZ3JXPaUiK1PmMw.png" /><figcaption>การทำงานของ yield return</figcaption></figure><p>ถึงแม้ว่าโค้ดคำสั่ง yield return จะถูกเขียนไว้กลาง block การทำงาน แต่จะยังไม่ return ค่า แล้วตัดจบการทำงานทันที แต่จะเป็นเหมือนการเอาค่าแอบไปเก็บไว้ก่อน รอจนกว่าจะจบคำสั่งการทำงาน หรือคำสั่ง break จึงค่อย return ค่าทั้งหมดออกไปทีเดียว ในตอนสุดท้ายนั่นเอง</p><p><strong>ยกตัวอย่าง</strong> ใช้ yield return ค่าตัวเลข 1, 2 และ 3</p><pre>IEnumerable&lt;int&gt; GetNumber()<br>{<br>    yield return 1;<br>    yield return 2;<br>    yield return 3;<br>}<br><br><br>//Output:✅<br>//1<br>//2<br>//3</pre><blockquote>ผลลัพธ์ ได้ค่าครบทั้ง 3 ค่า ไม่ได้ตัดจบตั้งแต่บรรทัด yield return 1; แต่อย่างใด</blockquote><p><strong>ข้อจำกัด</strong></p><ol><li>ไม่สามารถใช้กับ in, ref, หรือ out methods ได้</li><li>ไม่สามารถใช้กับ lambda expressions และ anonymous methods ได้</li><li>ไม่สามารถใช้กับ methods แบบ unsafe ได้</li></ol><p>เพียงเท่านี้ เพื่อนๆ ก็จะสามารถโค้ดเข้าถึงข้อมูล Collection สั้นลง อีกทั้งโค้ดก็ยังดู Clean มากขึ้นด้วย 👏</p><p>สุดท้ายนี้ ขอลาไปพร้อมกับน้องๆ Treasure เลยละกัน แล้วเจอกันใหม่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HJtsoeLb-Cb3e6nbew-pfg.jpeg" /></figure><blockquote>บาย 👋🏾👋🏾</blockquote><p>แหล่งที่มา</p><p><a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield">yield statement - provide the next element in an iterator - C#</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b4d7836f1ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาใช้ Nullish coalescing assignment (??=) กันเถอะ]]></title>
            <link>https://medium.com/exaler/%E0%B8%A1%E0%B8%B2%E0%B9%83%E0%B8%8A%E0%B9%89-nullish-assignment-operator-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-a8cf163d9940?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8cf163d9940</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[shorthand]]></category>
            <category><![CDATA[nullish-coalescing]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Sat, 27 May 2023 15:16:12 GMT</pubDate>
            <atom:updated>2023-06-27T08:13:53.525Z</atom:updated>
            <content:encoded><![CDATA[<p>ในภาษา JavaScript/Typescript การกำหนดค่าเริ่มต้น ให้กับตัวแปร หรือ Property ที่เป็น Nullish</p><p>สามารถทำได้ด้วยวิธีเหล่านี้</p><figure><img alt="มาใช้ Nullish coalescing assignment กันเถอะ — cover" src="https://cdn-images-1.medium.com/max/1024/1*zz6DWCHCPBl1T_bxyOXIbw.png" /><figcaption>ใช้ if | if/else กำหนดค่าเริ่มต้น</figcaption></figure><blockquote><em>🤔 </em>จะดีกว่าไหม ถ้าการทำงานเหมือนเดิม แต่โค้ดสั้นลง ?</blockquote><blockquote><em>“ มารู้จัก </em>Nullish coalescing assignment<strong><em> (??=) </em></strong><em>กัน “</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FTaC45zIUtytVUPdLEzWjA.png" /><figcaption><a href="https://twitter.com/nixcraft/status/1463365667881586689?s=20">https://twitter.com/nixcraft/status/1463365667881586689?s=20</a></figcaption></figure><h3>Nullish coalescing assignment คืออะไร❓</h3><blockquote>“Nullish coalescing assignment” (??=)</blockquote><blockquote><em>— เป็น</em>คำสั่ง<em> Shorthand ของภาษา JavaScript/Typescript</em></blockquote><blockquote><em>— ใส่เพื่อ</em>กำหนดค่าเริ่มต้นให้กับตัวแปร หรือ property เมื่อ<em>มีค่าเป็น null หรือ undefined (Nullish)</em></blockquote><h3><strong>รูปแบบ</strong></h3><figure><img alt="รูปแบบการใช้ Nullish coalescing assignment" src="https://cdn-images-1.medium.com/max/1024/1*K8nPgjVpVUNykWTpYtAnMw.png" /><figcaption>รูปแบบการใช้ Nullish coalescing assignment (??=)</figcaption></figure><ul><li><strong><em>x</em></strong> — ตัวแปร หรือ property ที่เป็น Nullish <br><em>(null | undefined)</em></li><li><strong><em>??=</em></strong> — คำสั่ง Nullish coalescing assignment</li><li><strong><em>y</em></strong> — ค่าที่จะกำหนด <br><em>(number, string, boolean, void, null, undefined, array, object, any etc.)</em></li></ul><pre>let x : number;<br>x ??= 0;      //0<br>10 - x;       </pre><h3>x ??= y ทำงานยังไง ✍️</h3><blockquote>“ y จะทำงานก็ต่อเมื่อ x มีค่าเป็น null | undefined เท่านั้น “</blockquote><p>เมื่อ x มีค่าเป็น null หรือ undefined (Nullish) x จะถูกกำหนดค่าเริ่มต้นให้เท่ากับ y ทันที</p><figure><img alt="x มีค่าเริ่มต้นเท่ากับ y" src="https://cdn-images-1.medium.com/max/190/1*jYQQuPlKTAE6f1EMIcmx0g.png" /><figcaption>x มีค่าเริ่มต้นเท่ากับ y</figcaption></figure><h3><strong>ตัวอย่าง</strong></h3><blockquote>ถึงแม้ x จะมีค่าเริ่มต้น (ไม่ใช่ Nullish) ก็สามารถใช้คำสั่ง ??= ได้ <br>แต่ x จะไม่ถูก assign ค่านั่นเอง</blockquote><pre>let x : number | void;<br>x = 1;<br>x ??= console.log(&quot;y evaluated&quot;);<br>//ไม่ Log อะไร เพราะ x มีค่า 1 (ไม่ใช่ Nullish)</pre><h3>รู้หมือไร่ 💡</h3><blockquote>x ??= y ย่อมาจาก x ?? (x = y)</blockquote><figure><img alt="มาใช้ Nullish assignment operator (??=) กันเถอะ — Shorthand" src="https://cdn-images-1.medium.com/max/600/1*A4AmeoJjOFYVu1UCF9qIQA.gif" /></figure><h3>มาทำ Workshop กัน❗️</h3><blockquote>โจทย์: ทำโปรแกรมคำนวณผลลัพธ์ของ 10 + x</blockquote><p>ประกาศตัวแปร x เป็น number</p><pre>//Bad ❌<br>let x : number;<br>10 + x;        //Error: Variable &#39;x&#39; is used before being assigned.<br>               //Error: &#39;x&#39; is possibly &#39;null&#39; or &#39;undefined&#39;.<br><br><br>//Good ✅<br>let x : number;<br>x ??= 0;          <br>10 + x;           //Ok<br>//or<br>10 + (x ??= 0);   //Ok</pre><p><em>x = 0 และ (10 + x) = 10 </em>✅</p><blockquote><strong>คำอธิบาย:</strong> x ไม่มีค่าเริ่มต้น (undefined) จะเอาไปคำนวณต่อไม่ได้ <br>ต้องกำหนดค่าเริ่มต้นให้ x = 0 ซะก่อน <br>ซึ่ง 10 + 0 ก็ได้ 10 เท่าเดิม</blockquote><h3>Tips 🚩 🚩</h3><blockquote>??= กับ ?? <strong>ไม่เหมือนกัน</strong></blockquote><figure><img alt="เปรียบเทียบ ??= กับ ??" src="https://cdn-images-1.medium.com/max/685/1*EB095Y9DpU82J6rM7JcTtg.png" /><figcaption>เปรียบเทียบ ??= กับ ??</figcaption></figure><ul><li>x ??= y — เมื่อ x เป็น null | undefined จะกำหนดค่าเริ่มต้นให้ x เท่ากับ y <em><br>(x มีค่าเริ่มต้น)</em></li><li>x ?? y — เมื่อ x เป็น null | undefined จะทำคำสั่ง y แทน x <br><em>(x ไม่มีค่าเริ่มต้น = undefined)</em></li></ul><h3>สรุป</h3><ul><li>Nullish coalescing assignment (??=) เป็นคำสั่ง Shorthand ของภาษา JavaScript/Typescript</li><li>ใส่เพื่อกำหนดค่าเริ่มต้นให้กับตัวแปร หรือ property เมื่อมีค่าเป็น null หรือ undefined (Nullish)</li><li>รูปแบบการใช้ x ??= y</li></ul><blockquote>จบ</blockquote><p><em>แต่!!!! </em>ก่อนไป ขอทิ้งคำถามกวนใจไว้หน่อย</p><p>จากรูป 👇 x มีค่าเท่าไร ?</p><figure><img alt="จงหาค่า x ดังต่อไปนี้" src="https://cdn-images-1.medium.com/max/965/1*epU6cZ2GTXYWwl1txWu_nQ.png" /><figcaption>จงหาค่า x ดังต่อไปนี้</figcaption></figure><blockquote>ไปจริงละ บาย👋👋</blockquote><p>แหล่งที่มา</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment">Nullish coalescing assignment (??=) - JavaScript | MDN</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8cf163d9940" width="1" height="1" alt=""><hr><p><a href="https://medium.com/exaler/%E0%B8%A1%E0%B8%B2%E0%B9%83%E0%B8%8A%E0%B9%89-nullish-assignment-operator-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-a8cf163d9940">มาใช้ Nullish coalescing assignment (??=) กันเถอะ</a> was originally published in <a href="https://medium.com/exaler">Exaler Co.,Ltd</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Truncate String ง่ายกว่าตัดใจจากเธอ]]></title>
            <link>https://medium.com/exaler/truncate-string-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B8%81%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%95%E0%B8%B1%E0%B8%94%E0%B9%83%E0%B8%88%E0%B8%88%E0%B8%B2%E0%B8%81%E0%B9%80%E0%B8%98%E0%B8%AD-5feb7807e63e?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/5feb7807e63e</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[tricks]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 09:18:35 GMT</pubDate>
            <atom:updated>2023-02-27T17:33:18.900Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Truncate String ง่ายกว่าตัดใจจากเธอ — Cover" src="https://cdn-images-1.medium.com/max/600/1*hYEnRbdiKXNR67e6XQZePA.gif" /></figure><p>สาย Frontend อย่างเรา น่าจะเคยพบเจอปัญหาเหล่านี้กันมาบ้าง</p><figure><img alt="Truncate String ง่ายกว่าตัดใจจากเธอ — Problem" src="https://cdn-images-1.medium.com/max/651/1*dqLxwwMvKkAu9JZHAN3u7Q.png" /><figcaption>ปัญหาข้อความไม่พอดี <em>Container</em></figcaption></figure><p><em>คือแบบว่า เอิ่ม…<br>ข้อความมันใหญ่เกิน Container ไปหน่อย <br>มันก็ดูไม่สวยใช่ไหมล่ะ?</em></p><blockquote>“ วันนี้เราจะพามาลองใช้ <strong>Ellipsis + </strong>Line Clamp เทคนิค CSS ง่ายๆ ที่จะช่วย Truncate String กัน ”</blockquote><h3>Ellipsis — Truncate String 1 บรรทัด</h3><ul><li>width : <em>yourWidth;</em><strong> </strong>— ระบุความกว้างคงที่</li><li>white-space : <em>nowrap; — </em>ให้ข้อความอยู่บรรทัดเดียวกัน</li><li>overflow<em> </em>:<em> hidden; </em>— ไม่ให้โชว์ Scrollbar</li><li>text-overflow :<strong> </strong><em>ellipsis; — </em>ตัดทอนข้อความ เติม … ต่อท้ายอัตโนมัติ</li></ul><h3>Line Clamp — Truncate String หลายบรรทัด</h3><ul><li>overflow : <em>hidden; — </em>ไม่ให้โชว์ Scrollbar</li><li>display : <em>-webkit-box; </em>— กำหนด Layout</li><li>-webkit-line-clamp : <em>number;</em> — จำนวนบรรทัดที่จะโชว์</li><li>-webkit-box-orient : <em>vertical | horizontal; </em>— แนวการตัดข้อความ</li></ul><h3>มาลองโค้ดกันเลย ❗️</h3><p>โดยระบุขนาด Container <br>width : <em>150px;</em><br>height : <em>50px;</em></p><pre>&lt;div&gt;<br>     &lt;div class=&quot;container&quot;&gt;<br>          &lt;h4&gt;<br>               ตัดอะไรก็ตัดได้ แต่อย่าตัดใจจากเราก็พอ<br>          &lt;/h4&gt;<br>     &lt;/div&gt;<br>&lt;/div&gt;</pre><p>ผลลัพธ์ที่ได้ ข้อความเกิน Container ออกมา</p><figure><img alt="Truncate String ง่ายกว่าตัดใจากเธอ — ผลลัพธ์ก่อน Truncate String 1 บรรทัด ด้วย Ellipsis" src="https://cdn-images-1.medium.com/max/488/1*OuHsyHVkNhGQwdNuG3znjA.gif" /><figcaption>ผลลัพธ์ก่อน Truncate String 1 บรรทัด ด้วย Ellipsis</figcaption></figure><p>แก้ปัญหาด้วย Ellipsis <br>เริ่มจากสร้าง Class .truncate ขึ้นมา โดยระบุตามนี้</p><pre>.truncate {<br>  width: 150px;  <br>  white-space: nowrap;<br>  overflow: hidden;<br>  text-overflow: ellipsis; <br>}</pre><p>เรียกใช้ Class .truncate</p><pre>&lt;div&gt;<br>     &lt;div class=&quot;container&quot;&gt;<br>          &lt;h4 class=&quot;truncate&quot;&gt;<br>               ตัดอะไรก็ตัดได้ แต่อย่าตัดใจจากเราก็พอ<br>          &lt;/h4&gt;<br>     &lt;/div&gt;<br>&lt;/div&gt;</pre><p>ผลลัพธ์ที่ได้ ข้อความถูกตัดทอนออกไป แล้วเติม … ต่อท้ายข้อความแทน</p><figure><img alt="Truncate String ง่ายกว่าตัดใจากเธอ — ผลลัพธ์หลัง Truncate String 1 บรรทัด ด้วย Ellipsis" src="https://cdn-images-1.medium.com/max/262/1*EGzC4WSQH1LrDQYeJhijrA.png" /><figcaption>ผลลัพธ์หลัง Truncate String 1 บรรทัด ด้วย Ellipsis</figcaption></figure><blockquote>Wow 😍😍😍</blockquote><p>ไหนลอง Truncate String หลายบรรทัด ด้วย Line Clamp ดู</p><pre>.truncate{<br>   overflow: hidden;<br>   display: -webkit-box;<br>   -webkit-line-clamp: 3;          /* โชว์ข้อความ 3 บรรทัด*/<br>   -webkit-box-orient: vertical;   /* ตัดคำแบบแนวนอน */<br>}</pre><p>ใช้งานกับเนื้อเพลงยาวๆ แบบนี้ ก็ได้ผลลัพธ์ที่น่าพอใจเช่นกัน</p><figure><img alt="Truncate String ง่ายกว่าตัดใจจากเธอ — ผลลัพธ์หลัง Truncate String หลายบรรทัด ด้วย Line Clamp" src="https://cdn-images-1.medium.com/max/1024/1*LpDu-BfgucQRoHXKpKhlPQ.png" /><figcaption>ผลลัพธ์หลัง Truncate String หลายบรรทัด ด้วย Line Clamp</figcaption></figure><blockquote>Perfect 👏👏👏</blockquote><h3>ข้อดี</h3><ul><li>สามารถตัดทอนข้อความผ่าน CSS ได้</li><li>โค้ดสั้น และเข้าใจง่าย</li></ul><h3><strong>ข้อควรระวัง</strong></h3><ul><li>ความกว้าง — ขนาดความกว้างไม่พอดี ทำให้ข้อความตกหล่นได้</li></ul><p>ปล. นอกจากวิธีข้างต้น สามารถ Truncate String ด้วยโค้ดฝั่ง Script ได้ เช่น</p><p>`${<strong><em>yourString</em></strong>.slice(0, <strong><em>numberLength</em></strong>)}…`;</p><h3><strong>สรุป</strong></h3><ul><li>Ellipsis ทำ Truncate String 1 บรรทัดผ่าน CSS ได้</li><li>Line Clamp ทำ Truncate String หลายบรรทัดผ่าน CSS ได้</li></ul><p>อย่างไรก็ตาม ถึงแม้การ Truncate String ด้วย เทคนิค CSS เหล่านี้จะง่าย แต่วิธีนี้อาจไม่ได้เหมาะกับทุกงาน และยังต้องระมัดระวังเรื่องการกำหนดความกว้างเป็นพิเศษอีกด้วย</p><p>แหล่งที่มา</p><ul><li><a href="https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/">Truncate String with Ellipsis | CSS-Tricks</a></li><li><a href="https://css-tricks.com/line-clampin/">Line Clampin&#39; (Truncating Multiple Line Text) | CSS-Tricks</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5feb7807e63e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/exaler/truncate-string-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B8%81%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%95%E0%B8%B1%E0%B8%94%E0%B9%83%E0%B8%88%E0%B8%88%E0%B8%B2%E0%B8%81%E0%B9%80%E0%B8%98%E0%B8%AD-5feb7807e63e">Truncate String ง่ายกว่าตัดใจจากเธอ</a> was originally published in <a href="https://medium.com/exaler">Exaler Co.,Ltd</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Invert “if” แค่คลิกเดียว]]></title>
            <link>https://medium.com/exaler/invert-if-%E0%B9%81%E0%B8%84%E0%B9%88%E0%B8%84%E0%B8%A5%E0%B8%B4%E0%B8%81%E0%B9%80%E0%B8%94%E0%B8%B5%E0%B8%A2%E0%B8%A7-75311bfb2040?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/75311bfb2040</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[c-sharp-language]]></category>
            <category><![CDATA[visual-basic-programming]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 00:13:27 GMT</pubDate>
            <atom:updated>2023-02-27T02:13:27.177Z</atom:updated>
            <content:encoded><![CDATA[<p><em>จะเกิดอะไรขึ้น ถ้าเราสามารถทำ Invert “if” ได้ง่ายๆ แค่คลิกเดียว 😻</em></p><figure><img alt="Invert “if” แค่คลิกเดียว — Cover" src="https://cdn-images-1.medium.com/max/1024/1*7JPfazksMKvXdpRVjr8mew.png" /></figure><blockquote>อย่างที่ทราบกันดีว่า Visual Studio มี option มากมาย ที่ช่วยให้เหล่าสาวกภาษา C# อย่างเราๆ Refactor โค้ดได้ง่ายดาย ผ่านเมาส์คลิก</blockquote><blockquote>และ 1 ในนั้น คือ <strong>“Invert if statement”</strong> นั่นเอง</blockquote><h3><strong>รู้จัก Invert if statement</strong></h3><blockquote><em>การ Refactor สลับคำสั่ง if หรือ if else ช่วยให้โค้ดดูเข้าใจง่ายมากขึ้น โดยคงการทำงานเดิมของโค้ดไว้</em></blockquote><figure><img alt="Invert “if” แค่คลิกเดียว — รู้จัก Invert if statement" src="https://cdn-images-1.medium.com/max/1024/1*0UBsevg206IX8RilWQ3ZKw.png" /><figcaption>Invert if statement concept</figcaption></figure><blockquote>Devlnwza007 said: บางครั้งการทำโค้ด if ซ้อน if หลายๆ ชั้น อาจทำให้โค้ดซับซ้อนเกินจำเป็น จนต้องเกิดการ Clean โค้ดตามมา ซึ่งถ้ามี Tools ที่มาช่วยจัดการ มันก็คงจะดีนะ 🤔</blockquote><h3>ทำ Invert if เมื่อไร ❓</h3><p>เมื่อต้องการ Refactor สลับโค้ดคำสั่ง if หรือ if else เพื่อให้โค้ดดูเข้าใจง่ายมากขึ้น</p><h3><strong>ทำไมต้อง </strong>Invert if ❓</h3><blockquote><strong>ประหยัดเวลา </strong>🕒 <strong>และช่วยลดข้อผิดพลาด </strong>✔️</blockquote><p>ลองคิดดูสิ ถ้าโค้ดคำสั่ง if หรือ if else ของเรามีความซับซ้อนมากๆ การ Maintain หรือการ Refactor โค้ดเหล่านั้นแต่ละที คงจะใช้เวลาน่าดู อีกทั้งยังอาจเกิดข้อผิดพลาดได้อีกด้วย</p><p>ดังนั้น เจ้า “Invert if” option นี้แหล่ะ คือฮีโร่ที่จะมาช่วยเราได้ภายในคลิกเดียว !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*42-zLPw7Ywda3w8YQ_3EOg.gif" /><figcaption>tenor.com</figcaption></figure><h3><strong>วิธีใช้งาน Invert if</strong></h3><ol><li>วาง cursor ที่ส่วนโค้ดคำสั่ง if หรือ if else</li></ol><figure><img alt="Invert “if” แค่คลิกเดียว — วาง cursor ที่ส่วนโค้ดคำสั่ง if หรือ if else" src="https://cdn-images-1.medium.com/max/791/1*X_qNh4r6I4jpCyT9ZaCVaQ.png" /></figure><p>2. กด Ctrl+. หรือคลิกสัญลักษณ์</p><figure><img alt="Invert “if” แค่คลิกเดียว — สัญลักษณ์" src="https://cdn-images-1.medium.com/max/48/1*9e2nPubOYiQfXl8g7hzt8A.png" /></figure><p>3. เลือก Invert if</p><figure><img alt="Invert “if” แค่คลิกเดียว — Step 1" src="https://cdn-images-1.medium.com/max/1024/1*G77pT-iWnirgaf0pqWOMOw.png" /></figure><h3>ผลลัพธ์</h3><figure><img alt="Invert “if” แค่คลิกเดียว — ตัวอย่างการใช้ Invert if สลับ if else" src="https://cdn-images-1.medium.com/max/1024/1*swOC7c4cI-JjyS2RKljTJA.png" /><figcaption>ตัวอย่างการใช้ Invert if สลับ if else</figcaption></figure><blockquote>จากตัวอย่าง จะเห็นว่าโค้ดคำสั่ง if else ถูกสลับที่กัน แต่การทำงานยังถูกต้องเหมือนเดิม</blockquote><p>ไหนลองกับโค้ดที่ซับซ้อนขึ้นสักนิด ซ้อน if ไปหน่อยๆ</p><figure><img alt="Invert “if” แค่คลิกเดียว — ตัวอย่างการใช้ Invert if ช่วยให้โค้ดดูง่ายขึ้น" src="https://cdn-images-1.medium.com/max/1024/1*Mj5pRhR29azHQML4TRgxyw.png" /><figcaption>ตัวอย่างการใช้ Invert if ช่วยให้โค้ดดูง่ายขึ้น</figcaption></figure><blockquote>จากตัวอย่าง<strong> </strong>โค้ดที่ได้ดูง่ายและชัดเจนมากขึ้น เพราะเมื่อ name เป็น null เราไม่ได้มีคำสั่งให้ทำงานอะไรต่อ จึงสามารถให้ออกจากเมธอดทันทีได้</blockquote><h3><strong>ข้อจำกัด</strong></h3><ul><li>ใช้สำหรับโค้ดคำสั่ง if หรือ if else เท่านั้น</li></ul><h3><strong>สรุป</strong></h3><ul><li>Invert if statement เป็นการ Refactor สลับโค้ดคำสั่ง if หรือ if else ให้โค้ดดูเข้าใจง่ายมากขึ้น</li><li>C# สามารถทำ Invert if ได้ง่ายๆ ผ่าน option ใน Visual Basic</li></ul><p>Tools ดีๆ ที่เอามาฝากกันในวันนี้ หวังว่าเพื่อนๆ จะลองนำไปปรับใช้กันดูนะ 👋👋</p><p>แหล่งที่มา</p><p><a href="https://learn.microsoft.com/en-us/visualstudio/ide/reference/invert-if-statement?view=vs-2022">Invert if statement - Visual Studio (Windows)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75311bfb2040" width="1" height="1" alt=""><hr><p><a href="https://medium.com/exaler/invert-if-%E0%B9%81%E0%B8%84%E0%B9%88%E0%B8%84%E0%B8%A5%E0%B8%B4%E0%B8%81%E0%B9%80%E0%B8%94%E0%B8%B5%E0%B8%A2%E0%B8%A7-75311bfb2040">Invert “if” แค่คลิกเดียว</a> was originally published in <a href="https://medium.com/exaler">Exaler Co.,Ltd</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[! ต่อท้าย คืออะไรใน TypeScript]]></title>
            <link>https://medium.com/exaler/%E0%B8%95%E0%B9%88%E0%B8%AD%E0%B8%97%E0%B9%89%E0%B8%B2%E0%B8%A2-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3%E0%B9%83%E0%B8%99-typescript-1038b9e04e6b?source=rss-d091a3d24911------2</link>
            <guid isPermaLink="false">https://medium.com/p/1038b9e04e6b</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[moonaarb]]></dc:creator>
            <pubDate>Mon, 30 Jan 2023 04:32:31 GMT</pubDate>
            <atom:updated>2023-02-26T15:43:05.754Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="! ต่อท้าย คืออะไรใน TypeScript — Thumb" src="https://cdn-images-1.medium.com/max/1024/1*ZDVZ700ijf8AkSA3gbgk8A.png" /></figure><p><em>คุณเคยเห็นเจ้าเครื่องหมายนี่ “!” ต่อท้ายในโค้ดบ้างไหม?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*JAPTFVu9Y6NrgTlFRF3hCA.png" /></figure><p><em>แล้วสงสัยไหมว่ามันคืออะไร? <br>ใส่ไว้ทำไม?</em></p><p><em>ถ้าคุณสงสัยเราคือเพื่อนกัน</em></p><h3><strong>StrictNullChecks</strong></h3><blockquote>เป็นโหมดการตรวจสอบค่า Null ที่เข้มงวด</blockquote><blockquote>ใน TypeScript 2.0 ตัว Compile จะมีโหมด<strong> </strong>StrictNullChecks เป็นโหมดการตรวจสอบ Null ที่เข้มงวดติดมาด้วย เพื่อป้องกันการใส่ค่า Null หรือ Undefined ให้กับตัวแปรที่ระบุ Data type ไว้แล้ว (non-null and non-undefined assertion)</blockquote><p>สามารถตั้งค่าโหมด strictNullChecks ได้ที่ <em>tsconfig.json</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7s3qt-MFsLeSLb5Ix3BImg.png" /><figcaption>ตั้งค่าโหมด strictNullChecks ใน tsconfig.json</figcaption></figure><h3>เครื่องหมาย ! ต่อท้าย คืออะไร?</h3><blockquote>เครื่องหมาย “!” (Exclamation Mark) เมื่อใส่ต่อท้ายค่าข้อมูล สามารถช่วยเรื่อง non-null and non-undefined assertion ได้</blockquote><blockquote>เรียกว่า “Non-null assertion operator”</blockquote><blockquote>ตัวอย่างเช่น กรณีประกาศตัวแปร Data type number ไว้ แต่ค่าจริงสามารถเป็นได้ทั้ง number, null และ <em>undefined</em> ได้นั่นเอง</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9EEhDy1ZgfOSvhd5x1CLpw.png" /><figcaption>ตัวอย่างการใช้ Non-null assertion operator</figcaption></figure><h3><strong>การทำงาน</strong></h3><blockquote>คอมไพเลอร์ละเว้น Error นี้ทันที</blockquote><blockquote>Non-null assertion operator นี้ จะเป็นตัวบอกคอมไพเลอร์ ที่โหมด StrictNullChecks ว่าฉันมีค่าแล้วจริงๆ นะ ฉันไม่ผิด ให้ข้าม Error นี้ไปได้เลย</blockquote><h3><strong>การใช้งาน</strong></h3><ul><li>โหมด strictNullChecks</li><li>โหมดปกติ</li></ul><h3>มาลองโค้ดกัน !!!</h3><p>ยกตัวอย่าง แอพของเราต้องการเก็บค่า string สักค่านึง <em>แต่!!!!!!!!</em> ปัญหาคือเราดันไม่รู้ว่าค่าที่รับมาจริงๆ นั้น มีโอกาสเป็น null หรือ undefined ได้หรือไม่</p><pre>let str : string; <br>str = &#39;Hello&#39;;     //Ok<br>str = null;        //Error : Type &#39;null&#39; is not assignable to type &#39;string&#39;.<br>str = undefined;   //Error : Type &#39;undefined&#39; is not assignable to type &#39;string&#39;.<br>let x : null = null;<br>let y : undefined = undefined;<br>str = x;           //Error : Type &#39;null&#39; is not assignable to type &#39;string&#39;.<br>str = y;           //Error : Type &#39;undefined&#39; is not assignable to type &#39;string&#39;.</pre><p>Error เมื่อใส่ค่า null หรือ undefined ให้กับตัวแปร Data type string</p><blockquote><strong>วิธีแก้</strong> ถ้าเจอ Error ข้างต้น ให้ใส่เครื่องหมาย ! ต่อท้ายค่า null หรือ undefined ทันที</blockquote><p>จะได้โค้ดสวยงามไร้ Error แบบนี้</p><pre>let str : string;<br>str = &#39;Hello&#39;;                   //Ok<br>str = null!;                     //Ok <br>str = undefined!;                //Ok<br>let x : null = null;<br>let y : undefined = undefined;<br>str = x!;                        //Ok<br>str = y!;                        //Ok</pre><p>เรียบร้อย ไม่ยากเลยใช่ไหมล่ะ…</p><p>ปล. นอกจากวิธีข้างต้น เราสามารถแก้ปัญหานี้ด้วยวิธีอื่นได้ <br>เช่น<br>1. ระบุ Data type ให้ครบ ว่าค่าของเราสามารถเป็นอะไรได้บ้าง</p><pre>let str : string | null | undefined | &#39;Hello&#39;;</pre><p>2. ปิดโหมด strictNullChecks</p><h3>สรุป</h3><ul><li>เครื่องหมาย “!” ต่อท้ายค่า เรียกว่า “Non-null assertion operator”</li><li>สามารถช่วยเรื่อง non-null and non-undefined assertion ได้</li></ul><p>อย่างไรก็ตาม ถึงแม้การใช้ Non-null assertion operator นี้ จะช่วย Developer ประหยัดเวลาโค้ดได้มาก แต่ยังต้องระมัดระวังในการใช้งาน และควรคำนึงถึงความปลอดภัยด้วยเช่นกัน</p><p>แหล่งที่มา</p><p><a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#example">Documentation - TypeScript 2.0</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1038b9e04e6b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/exaler/%E0%B8%95%E0%B9%88%E0%B8%AD%E0%B8%97%E0%B9%89%E0%B8%B2%E0%B8%A2-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3%E0%B9%83%E0%B8%99-typescript-1038b9e04e6b">! ต่อท้าย คืออะไรใน TypeScript</a> was originally published in <a href="https://medium.com/exaler">Exaler Co.,Ltd</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>