<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Javascript on NewInWeb</title>
    <link>//newinweb.menardi.dev/tags/javascript/</link>
    <description>Recent content in Javascript on NewInWeb</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 01 Sep 2020 11:00:00 +0000</lastBuildDate>
    
        <atom:link href="//newinweb.menardi.dev/tags/javascript/index.xml" rel="self" type="application/rss+xml" />
    
    
    
    <item>
      <title>String.replaceAll</title>
      <link>//newinweb.menardi.dev/2020/09/01/string-replaceall/</link>
      <pubDate>Tue, 01 Sep 2020 11:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2020/09/01/string-replaceall/</guid>
      <description>&lt;p&gt;Something that often catches out developers when doing string replacement in Javascript is that it is not global by default. That is, when replacing a substring, only the first instance is replaced:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;str&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;Welcome to WEBSITE. We hope you enjoy WEBSITE.&amp;#39;&lt;/span&gt;;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;console&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;log&lt;/span&gt;(&lt;span style=&#34;color:#a6e22e&#34;&gt;str&lt;/span&gt;.&lt;span style=&#34;color:#a6e22e&#34;&gt;replace&lt;/span&gt;(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;WEBSITE&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;newinweb.menardi.dev&amp;#39;&lt;/span&gt;));
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// &amp;#39;Welcome to newinweb.menardi.dev. We hope you enjoy WEBSITE.&amp;#39;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Previously, to replace all instances of &lt;code&gt;WEBSITE&lt;/code&gt; above, you would need to create a Regex, and set the &lt;code&gt;g&lt;/code&gt; flag to make it global.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Relative Time strings</title>
      <link>//newinweb.menardi.dev/2019/01/15/relative-time/</link>
      <pubDate>Tue, 15 Jan 2019 11:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2019/01/15/relative-time/</guid>
      <description>&lt;p&gt;Handling dates in Javascript has always been considered a tricky task, which led to the rise of date-handling libraries such as &lt;a href=&#34;https://momentjs.com/&#34;&gt;Moment.js&lt;/a&gt;. One of the most common uses of Moment.js is converting dates to relative time, such as &amp;ldquo;a few seconds ago&amp;rdquo; or &amp;ldquo;a month from now&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Now this can be partially achieved natively in browsers using &lt;code&gt;Intl.RelativeTimeFormat&lt;/code&gt;. It takes an amount of time as a number, and formats it as a relative time in the user&amp;rsquo;s language. This offers a great space saving as you will no longer need to include extra language files on your site to handle all these cases.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Improved Performance with OffscreenCanvas</title>
      <link>//newinweb.menardi.dev/2018/09/10/offscreen-canvas/</link>
      <pubDate>Mon, 10 Sep 2018 11:45:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/09/10/offscreen-canvas/</guid>
      <description>&lt;p&gt;When doing advanced animations or illustrations on the web, many developers opt to use &lt;em&gt;canvas&lt;/em&gt;. It is extremely flexible and a great choice for many projects. However, one serious drawback is that it renders on the same thread as the rest of the UI. So, if you have intensive canvas calculations, then scrolling, typing and other interactions with your page can become janky and slow.&lt;/p&gt;
&lt;p&gt;With the introduction of OffscreenCanvas, it is now possible to render a canvas using a web worker, meaning that all the heavy lifting is done on a separate thread.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Shadow DOM: Encapsulation for Custom Elements</title>
      <link>//newinweb.menardi.dev/2018/05/09/shadow-dom/</link>
      <pubDate>Wed, 09 May 2018 09:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/05/09/shadow-dom/</guid>
      <description>&lt;p&gt;&lt;em&gt;This post is part of a series on Custom Elements. See the &lt;a href=&#34;https://newinweb.menardi.dev/2018/04/29/custom-elements/&#34;&gt;first post in the series&lt;/a&gt; for an introduction.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Custom elements are a great way of creating self-contained elements, and used in many frameworks, such as &lt;a href=&#34;https://onsen.io&#34;&gt;Onsen UI&lt;/a&gt; and &lt;a href=&#34;https://ionicframework.com&#34;&gt;Ionic&lt;/a&gt;. But, how do they ensure that the user&amp;rsquo;s own CSS doesn&amp;rsquo;t accidentally interfere with the framework&amp;rsquo;s CSS? This is where the shadow DOM comes in.&lt;/p&gt;
&lt;h2 id=&#34;css-leaking&#34;&gt;CSS Leaking&lt;/h2&gt;
&lt;p&gt;In our original &lt;a href=&#34;https://codepen.io/newinweb/pen/odBwVL&#34;&gt;custom elements example&lt;/a&gt;, we had a simple series of messages which used a custom element called &lt;code&gt;custom-message&lt;/code&gt;. Inside this element, we automatically set up a &lt;code&gt;div&lt;/code&gt; with the class &lt;code&gt;message&lt;/code&gt;. Imagine that we publish our &lt;code&gt;custom-message&lt;/code&gt; to be used by any developer who wants to use it, such as in Onsen UI or Ionic. For simplicity, we move the CSS into a &lt;code&gt;style&lt;/code&gt; tag inside the component so it is completely standalone.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Custom Elements (Web Components)</title>
      <link>//newinweb.menardi.dev/2018/04/29/custom-elements/</link>
      <pubDate>Sun, 29 Apr 2018 09:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/04/29/custom-elements/</guid>
      <description>&lt;p&gt;Custom Elements (also known as Web Components) are a great way of writing reusable and testable code. They allow you to define a new HTML tag, which has Javascript associated with it. If you have used React or Vue before, then these will feel familiar to Components in those libraries. Custom Elements will also be easy to pick up if you are familiar with Angular directives.&lt;/p&gt;
&lt;h2 id=&#34;where-can-i-use-one&#34;&gt;Where can I use one?&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s take the simple case of a messaging app, and consider what the markup for a two simple text messages might look like.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Fetch: A modern replacement for XHR</title>
      <link>//newinweb.menardi.dev/2018/03/26/fetch-a-modern-replacement-for-xhr/</link>
      <pubDate>Mon, 26 Mar 2018 10:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/03/26/fetch-a-modern-replacement-for-xhr/</guid>
      <description>&lt;p&gt;If you have ever had to fetch some JSON from a website without using a library, you will be familiar with &lt;code&gt;XMLHttpRequest&lt;/code&gt;. It can be quite tedious to use, which is why all modern browsers now support the much easier &lt;code&gt;fetch&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Fetch offers a simple way of making requests to a server, returning the result in a promise. In the simple example below, we are getting the HTML of the home page of NewInWeb using both XHR and fetch, and logging it out.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Javascript Arrow functions</title>
      <link>//newinweb.menardi.dev/2018/03/20/javascript-arrow-functions/</link>
      <pubDate>Tue, 20 Mar 2018 09:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/03/20/javascript-arrow-functions/</guid>
      <description>&lt;p&gt;One of the biggest new features in the ES2015 specification was the introduction of arrow functions. You have probably already seen these around the internet, and indeed in my previous blog posts. They help to make function definitions much shorter, as well as making working with &lt;code&gt;this&lt;/code&gt; much easier.&lt;/p&gt;
&lt;h2 id=&#34;shorter-function-definitions&#34;&gt;Shorter function definitions&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with the basics. How do you define an arrow function? In the simplest case, remove the &lt;code&gt;function&lt;/code&gt; keyword, and add &lt;code&gt;=&amp;gt;&lt;/code&gt; after the list of arguments. In the example below, these functions do the same thing, adding x and y together.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>How and why to use Symbols in Javascript</title>
      <link>//newinweb.menardi.dev/2018/03/14/how-to-use-symbols/</link>
      <pubDate>Wed, 14 Mar 2018 19:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/03/14/how-to-use-symbols/</guid>
      <description>&lt;p&gt;Symbols are a primitive data type in Javascript introduced in the ES2015 specification. It is the seventh data type in Javascript, joining &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt;. The use case for &lt;code&gt;Symbol&lt;/code&gt; is relatively limited, but it is worth knowing about since it is one of the few primitive data types.&lt;/p&gt;
&lt;h2 id=&#34;how-to-use-symbol&#34;&gt;How to Use Symbol&lt;/h2&gt;
&lt;p&gt;A symbol is created by calling the &lt;code&gt;Symbol&lt;/code&gt; function. It accepts one optional parameter, which is a &lt;code&gt;description&lt;/code&gt;. It&amp;rsquo;s important to realise that &lt;em&gt;the only purpose of the description is to aid in debugging&lt;/em&gt;. That is, when you &lt;code&gt;console.log&lt;/code&gt; your symbol, it will also show the description. The description does not affect the operation of the symbol itself in any way.&lt;/p&gt;</description>
    </item>
    
    
    
    <item>
      <title>Strings in ES2015&#43;</title>
      <link>//newinweb.menardi.dev/2018/03/10/strings-in-es2015/</link>
      <pubDate>Sat, 10 Mar 2018 18:00:00 +0000</pubDate>
      
      <guid>//newinweb.menardi.dev/2018/03/10/strings-in-es2015/</guid>
      <description>&lt;p&gt;Today we will take a look at some new string-related features, which you can start using in modern browsers right now. These are template literals, .includes(), padding and repeat.&lt;/p&gt;
&lt;h2 id=&#34;template-literals-mdn&#34;&gt;Template literals &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals&#34;&gt;MDN&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Template literals are one of my favourite recent Javascript features. If you find yourself constructing strings from variables frequently, you may be familiar with how unwieldy such a construction can become. The new template literals introduce the use of ` (backticks) instead of &lt;code&gt;&#39;&lt;/code&gt; and &lt;code&gt;&amp;quot;&lt;/code&gt;. To use variables inside template literals, you simply include it like &lt;code&gt;${variable}&lt;/code&gt;. It is worth knowing that inside &lt;code&gt;${...}&lt;/code&gt;, you can put any Javascript code you want, not just variables!&lt;/p&gt;</description>
    </item>
    
    
  </channel>
</rss>
