<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: WEI FENG</title>
    <description>The latest articles on DEV Community by WEI FENG (@weifengnusceg).</description>
    <link>https://dev.to/weifengnusceg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F647945%2F169343ce-7e63-4256-9853-ab490f48a094.jpeg</url>
      <title>DEV Community: WEI FENG</title>
      <link>https://dev.to/weifengnusceg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/weifengnusceg"/>
    <language>en</language>
    <item>
      <title>Javascript method Implementation</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Tue, 07 Dec 2021 14:35:54 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/javascript-method-reproduce-1l2f</link>
      <guid>https://dev.to/weifengnusceg/javascript-method-reproduce-1l2f</guid>
      <description>&lt;p&gt;"Let's take a deep dive to the most basic javascript method implementation. Hope it will help you to understand the language better"&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;1. Object.create()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newCreate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fucntion&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// create a new function object as &lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;

    &lt;span class="c1"&gt;//bind the function object's prototype to the obj&lt;/span&gt;
    &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;

    &lt;span class="c1"&gt;//return the newly created object based on F()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. instanceof&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myInstanceOf&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//access left object's prototype chain&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;leftProto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;
    &lt;span class="c1"&gt;//access right constructor's prototype method&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rightProto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;

    &lt;span class="c1"&gt;//check if rightProto is on the left object's prototype chain&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftProto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftProto&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;rightProto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;leftProto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leftProto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. new&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myNew&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//create a empty object &lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="c1"&gt;//link the new object's prototype chain to the constructor's prototype&lt;/span&gt;
    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;
    &lt;span class="c1"&gt;//call the constructor function on new object.&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. promise.all()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//takes in an array of promises&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myPromiseAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="c1"&gt;// return a new promise&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
        &lt;span class="c1"&gt;// setup a array to record the result&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;resolvedCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="c1"&gt;//wrap the value from array in case it is not a promise &lt;/span&gt;
           &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
               &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
               &lt;span class="c1"&gt;//resolve the result when finish resolving everything &lt;/span&gt;
               &lt;span class="nx"&gt;resolvedCount&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
              &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolvedCount&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="c1"&gt;// if there is an error, reject immediately&lt;/span&gt;
             &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;})&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. promise.race()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//takes in an array of promises&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myPromiseRace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
           &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Debounce function&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The debounce() function forces a function to wait a certain amount of time before running again. The function is built to limit the number of times a function is called.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// set a timer variable in the clousure to keep track&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   &lt;span class="c1"&gt;//return a debounced function&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounced&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//if timer is not null, clear the timer&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;//set a delay to run the function&lt;/span&gt;
        &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. Throttle function&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To throttle a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds). This means throttling will prevent a function from running if it has run “recently”. Throttling also ensures a function is run regularly at a fixed rate.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;throttle&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="c1"&gt;// set a flag in clousure to decide whether the current throttled program is running&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;throttled&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// if the flag is true, call the function and set the flag to false&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
            &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//set the flag back to true once finished waiting&lt;/span&gt;
                &lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
                &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8. Function.prototype.call()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The call() method calls a function with a given this value and arguments provided individually. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Function.prototype.apply() is similar to call, we just need to change the seperate arguments to array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newCall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// this is pointing to the function that call this newCall method, when we assign this function as context's property, this will point to the context.&lt;/span&gt;
    &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;9. Function.prototype.bind()&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
    &lt;span class="c1"&gt;//save the rest of the parameters&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;binded&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//concat the arguments&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;10. a function to determine the type of an object&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using toString() to detect object class&lt;br&gt;
toString() can be used with every object and (by default) allows you to get its class.&lt;br&gt;
To use the Object.prototype.toString() with every object, you need to call Function.prototype.call() or Function.prototype.apply() on it, passing the object you want to inspect as the first parameter (called thisArg).&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;typeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// use array destructuring to extract the result&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//slice out unnecessary characters&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;11. curried function&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.&lt;br&gt;
Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).&lt;br&gt;
Currying doesn’t call a function. It just transforms it.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;curry&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;curried&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//compare the current arguments length with the function's required parameters. &lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// concat the rest of the arguments&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;curried&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;12. AJAX request&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AJAX update a web page without reloading the page&lt;br&gt;
Request data from a server - after the page has loaded&lt;br&gt;
Receive data from a server - after the page has loaded&lt;br&gt;
Send data to a server - in the background&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;xhr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onreadystatechange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="c1"&gt;// if readyState is 4 and status 200 means the request has been sucessfully executed. We can proceed to handle the response.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readyState&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;wrap the ajax in a promise&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ajaxPromise&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;xhr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onreadystatechange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readyState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;13. Shallow Copy&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using slice
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// [1,2,3,4]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;use spread operator
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
       &lt;span class="na"&gt;second&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cloneObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;14. Deep Copy&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;handwritten deepcopy
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;deepCopy&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

     &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;newObj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="nx"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newObj&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;JSON.parse(JSON.stringify(obj))&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But this method has a problem. If there are functions, undefined, and symbols in the copied object, they will disappear after processing with JSON.stringify().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. randomize the item sequence in array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;randomOutput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;randomNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;randomNum&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;randomNum&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;16. How to flatten array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;recursively flatten the array use array reduce method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;flatten&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="nx"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},[])&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;17. Event publisher and subscriber model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;this is not a complete implementation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;emit&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;off&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialCallback&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;once&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;off&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initialCallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;callbackFn&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eventName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;18. Inherit from parent constructor&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Man&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Human&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;Man&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Human&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;Man&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Man&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;19.Convert Json to Tree&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
          &lt;span class="p"&gt;}]&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;toTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ele&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
            &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;


&lt;span class="c1"&gt;//Use React.createElement API&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;20.Quick Sort&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;quickSort1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//find the mid index&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;midIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//retrieve the mid index value&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;valArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;midIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;midIndexVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;valArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;//save those smaller than midIndexVal&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;//save those bigger than midIndexVal&lt;/span&gt;
    &lt;span class="c1"&gt;//iterate the array, push them into respective side. &lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;midIndexVal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//recursively sort the array until array length &amp;lt; 1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;quickSort1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;midIndexVal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quickSort1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;quickSort1 &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quickSort1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// quickSort1: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;21.Merge Sort&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eyUv4Uja--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3u0umfadm8iawgiiibn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eyUv4Uja--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3u0umfadm8iawgiiibn.png" alt="Image description" width="562" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22.Create Element tree with react&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;child&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Web Optimization Concepts - The one and only guide you need (In progress)</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Mon, 06 Dec 2021 19:52:57 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch</link>
      <guid>https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch</guid>
      <description>&lt;p&gt;"How to fully optimize web performance? You'll understand after reading this article"&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. What is and how to use CDN&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;1.1 What is CDN exactly?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1.2 What's the benefits of CDN&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2. Why do we need Lazy Load&lt;br&gt;
3. Reflow and Repaint&lt;/p&gt;



&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki"&gt;React Concepts Part.1 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib"&gt;React Concepts Part.2 - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6"&gt;CSS Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k"&gt;Computer Network Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni"&gt;Browser Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What is CDN and how to apply CDN &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.1 What is CDN exactly?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CDN (Content Delivery Network) refers to a computer network system connected to each other through the closest server. So that content such as music, pictures, videos, applications and other static resources can be send to users more reliable and efficiently. CDN provides high-performance, scalability and low-cost network content to users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.2 What's the benefits of CDN&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In terms of performance, the role of introducing CDN is to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Users receive content from the nearest server, with lower latency , faster content loading speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Part of the resource request is allocated to the CDN, reducing the load on the server&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In terms of network security, CDN helps defend against DDoS, MITM and other network attacks.&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Why do we need Lazy Load &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Lazy loading is also called on-demand loading. It refers to the lazy loading of image data in long page website. &lt;/p&gt;

&lt;p&gt;In a relatively long web page, if many pictures all loaded at the same time but the user can only see the part of the picture in the viewport, it wastes a lot of performance.&lt;/p&gt;

&lt;p&gt;The above problems can be solved. Before scrolling the screen, pictures outside the visualization area will not be loaded, it will only be loaded when it shows up in the viewport. This makes the page load faster and reduces the load on the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we achieve lazy load&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In HTML5, &lt;code&gt;image&lt;/code&gt; tag has an attribute call &lt;code&gt;data-src&lt;/code&gt; to temporarily store resource path as we would not want it to be load immediately. After that we will keep monitoring the relative postion between the image and viewport. Once it appears in the viewport, we will switch the data in &lt;code&gt;data-src&lt;/code&gt; to &lt;code&gt;src&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are two ways to monitoring the relative postion between the image and viewport.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. Calculate the height manually&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;window.innerHeight&lt;/code&gt;&lt;br&gt;
The read-only innerHeight property of the Window interface returns the interior height of the window in pixels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;document.body.scrollTop&lt;/code&gt;&lt;br&gt;
The Element.scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;imgs.offsetTop&lt;/code&gt;&lt;br&gt;
The HTMLElement.offsetTop read-only property returns the distance of the outer border of the current element relative to the inner border of the top of the offsetParent node.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if &lt;code&gt;img.offsetTop &amp;lt; window.innerHeight + document.body.scrollTop&lt;/code&gt;, it indicates that the image has entered viewport&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading.gif&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pic.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading.gif&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pic.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading.gif&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pic.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;imgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;lazyLoad&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;winHeight&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;imgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imgs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;offsetTop&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;winHeight&lt;/span&gt; &lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="nx"&gt;imgs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;imgs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;data-src&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onscroll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lazyLoad&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. Intersection Observer API&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API"&gt;Read more about implementation about Intersection Observer API&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.&lt;/p&gt;

&lt;p&gt;The Intersection Observer API allows you to configure a callback that is called when either of these circumstances occur:&lt;/p&gt;

&lt;p&gt;A target element intersects either the device's viewport or a specified element. That specified element is called the root element or root for the purposes of the Intersection Observer API.&lt;br&gt;
The first time the observer is initially asked to watch a target element.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Reflow and Repaint &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/swlh/what-the-heck-is-repaint-and-reflow-in-the-browser-b2d0fb980c08"&gt;read more about it here&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Repaint&lt;/strong&gt;&lt;br&gt;
As the name suggests repaint is nothing but the repainting element on the screen as the skin of element change which affects the visibility of an element but do not affects layout.&lt;/p&gt;

&lt;p&gt;Example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Changing visibility of an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changing an outline of the element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changing background.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Would trigger a repaint.&lt;br&gt;
According to Opera, the repaint is an expensive operation as it forces the browser to verify/check the visibility of all other dom nodes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Reflow&lt;/strong&gt;&lt;br&gt;
Reflow means re-calculating the positions and geometries of elements in the document, for the purpose of re-rendering part or all of the document. Because reflow is a user-blocking operation in the browser, it is useful for developers to understand how to improve reflow time and also to understand the effects of various document properties (DOM depth, CSS rule efficiency, different types of style changes) on reflow time. Sometimes reflowing a single element in the document may require reflowing its parent elements and also any elements which follow it.&lt;/p&gt;

&lt;p&gt;Repaints and reflows can be expensive, they can hurt the user experience, and make the UI appear sluggish&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to reduce reflow and repaint?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When manipulating the DOM, try to operate on lower-level DOM nodes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use absolute or fixed to keep elements out of the document flow so that their changes will not affect other elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To avoid frequent manipulation of the DOM, you can create a &lt;code&gt;documentFragment&lt;/code&gt;, apply all DOM operations on it, and finally update it to the document. Why not use React?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to optimize Animation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Same as above mentioned. Use absolute or fixed to keep elements out of the document flow so that their changes will not affect other elements.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Browser Concepts - The one and only guide you need (In progress)</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Sat, 04 Dec 2021 13:00:47 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni</link>
      <guid>https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni</guid>
      <description>&lt;p&gt;"How can our web application run without the browser? That's a good question.. Here are some useful concepts and articles i think a web developer should read regarding browsers. I know i know this is very tedious but trust me some day you will need this :)"&lt;br&gt;
&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h0pnfR8K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk62lqvg50cimd5ed692.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h0pnfR8K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk62lqvg50cimd5ed692.png" alt="Image description" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. Browser Safety&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;1.1 Cross Site Scripting attack(XSS)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1.2 Cross-site request forgery(CSRF)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;1.3 What is a man-in-the-middle attack?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2. Thead and process&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;2.1 The difference between thread and process&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.2 Service Worker&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3. The mechanism behind browser cache&lt;/p&gt;

&lt;p&gt;4. Cookie, Session and LocalStorage&lt;/p&gt;

&lt;p&gt;5. Browser same origin policy&lt;/p&gt;

&lt;p&gt;6. Garbage Collection in Javascript V8 &lt;/p&gt;

&lt;p&gt;7. How to Keep user logged in credential&lt;/p&gt;

&lt;p&gt;8. What happened when Access-Control-Allow-Origin is * (wild card)&lt;/p&gt;

&lt;p&gt;9. Communicate Across Browser Tabs&lt;/p&gt;



&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki"&gt;React Concepts Part.1 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib"&gt;React Concepts Part.2 - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6"&gt;CSS Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch"&gt;Web Optimization Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k"&gt;Computer Network Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Browser Safety &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1.1 Cross Site Scripting attack(XSS) &lt;a&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://portswigger.net/web-security/cross-site-scripting"&gt;For more information please refer to this link&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is cross-site scripting (XSS)?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cross-site scripting (also known as XSS) is a web security vulnerability that allows an attacker to compromise the interactions that users have with a vulnerable application. It usually happens because the website does not filter malicious code, it is mixed with normal code, and the browser has no way to distinguish which scripts are trustworthy, which leads to the execution of malicious code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does XSS work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cross-site scripting works by manipulating a vulnerable web site so that it returns malicious JavaScript to users if the website does not filter the received data. When the malicious code executes inside a victim's browser, the attacker can fully compromise their interaction with the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Three types of XSS attack:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Reflected cross-site scripting:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reflected XSS is the simplest variety of cross-site scripting. It arises when an application receives data in an HTTP request and includes that data within the immediate response in an unsafe way.&lt;/p&gt;

&lt;p&gt;Here is a simple example of a reflected XSS vulnerability:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://insecure-website.com/status?message=All+is+well"&gt;https://insecure-website.com/status?message=All+is+well&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p&amp;gt;Status: All is well.&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The application doesn't perform any other processing of the data, so an attacker can easily construct an attack like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://insecure-website.com/status?message="&gt;https://insecure-website.com/status?message=&lt;/a&gt;/&amp;lt;em&amp;gt;+Bad+stuff+here...+&amp;lt;/em&amp;gt;/&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p&amp;gt;Status: &amp;lt;script&amp;gt;/* Bad stuff here... */&amp;lt;/script&amp;gt;&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the user visits the URL constructed by the attacker, then the attacker's script executes in the user's browser, in the context of that user's session with the application. At that point, the script can carry out any action, and retrieve any data, to which the user has access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- DOM-based cross-site scripting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DOM-based XSS (also known as DOM XSS) arises when an application contains some client-side JavaScript that processes data from an untrusted source in an unsafe way, usually by writing the data back to the DOM.&lt;/p&gt;

&lt;p&gt;In the following example, an application uses some JavaScript to read the value from an input field and write that value to an element within the HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;search&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;results&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You searched for: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the attacker can control the value of the input field, they can easily construct a malicious value that causes their own script to execute:&lt;/p&gt;

&lt;p&gt;You searched for: &lt;code&gt;&amp;lt;img src=1 onerror='/* Bad stuff here... */'&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a typical case, the input field would be populated from part of the HTTP request, such as a URL query string parameter, allowing the attacker to deliver an attack using a malicious URL, in the same manner as reflected XSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Stored cross-site scripting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stored XSS (also known as persistent or second-order XSS) arises when an application receives data from an untrusted source and includes that data within its later HTTP responses in an unsafe way.&lt;/p&gt;

&lt;p&gt;The data in question might be submitted to the application via HTTP requests; for example, comments on a blog post, user nicknames in a chat room, or contact details on a customer order. In other cases, the data might arrive from other untrusted sources; for example, a webmail application displaying messages received over SMTP, a marketing application displaying social media posts, or a network monitoring application displaying packet data from network traffic.&lt;/p&gt;

&lt;p&gt;Here is a simple example of a stored XSS vulnerability. A message board application lets users submit messages, which are displayed to other users:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p&amp;gt;Hello, this is my message!&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The application doesn't perform any other processing of the data, so an attacker can easily send a message that attacks other users:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;p&amp;gt;&amp;lt;script&amp;gt;/* Bad stuff here... */&amp;lt;/script&amp;gt;&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to prevent XSS attacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Preventing cross-site scripting is trivial in some cases but can be much harder depending on the complexity of the application and the ways it handles user-controllable data.&lt;/p&gt;

&lt;p&gt;In general, effectively preventing XSS vulnerabilities is likely to involve a combination of the following measures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Filter input on arrival. At the point where user input is received, filter as strictly as possible based on what is expected or valid input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Encode data on output. At the point where user-controllable data is output in HTTP responses, encode the output to prevent it from being interpreted as active content. Depending on the output context, this might require applying combinations of HTML, URL, JavaScript, and CSS encoding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use appropriate response headers. To prevent XSS in HTTP responses that aren't intended to contain any HTML or JavaScript, you can use the Content-Type and X-Content-Type-Options headers to ensure that browsers interpret the responses in the way you intend. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Content Security Policy. As a last line of defense, you can use Content Security Policy (CSP) to reduce the severity of any XSS vulnerabilities that still occur.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1.2 Cross-site request forgery(CSRF) &lt;a&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://portswigger.net/web-security/csrf"&gt;Refer to this link for more info &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is CSRF?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A CSRF attack refers to a cross-site request forgery attack. The attacker induces a user to enter a third-party website, and then the website sends a cross-site request to the targeted website and    bypass the same-origin policy.&lt;/p&gt;

&lt;p&gt;The key point of the CSRF attack is to impersonate the user by hijack the cookie as cookie will be carried in the same-origin request and sent to the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to prevent CSRF attack&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Perform same-origin detection:&lt;/strong&gt; The server should decide whehter the request is allowed based on the request header's origin or referer section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Use CSRF token:&lt;/strong&gt; The server returns a random number Token to the user. When user sends another request, the token will be add to into the request header, and the server verifies this token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Set the samesite attribute in Cookie:&lt;/strong&gt; It will not allow the cookie to be used by third parties, so as to avoid being exploited by attackers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.3 What is a man-in-the-middle attack? &lt;a&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Man-in-the-middle attack (MITM) refers to the fact that the attacker establishes a unique connection with both ends of the communication, and exchanges the data they receive, so that both ends of the communication think they are passing through a private connection and a direct conversation with each other, but in fact the entire conversation is completely controlled by the attacker. In a man-in-the-middle attack, the attacker can intercept the two-way communication and insert new content.&lt;/p&gt;

&lt;p&gt;The attack process is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The client sends a request to the server, and the request is intercepted by the middleman&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server sends the public key to the client&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The middleman intercepts the public key and keeps it on his own. Then create a fake public key and send it to the client&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the client receives the fake public key, it generates an encrypted hash value and sends it to the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The attacker obtains the encrypted hash value, decrypts it with its own private key to obtain the true key, and at the same time generates a fake encrypted hash value and sends it to the server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server decrypts with the private key to obtain the fake key, and then encrypts the data and transmits it to the client&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What may cause front-end security issues?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Other than the above mentioned, we should avoid abusively using iframe. The content inside iframe is provided by the third party. By default, they are not controlled. They can run scripts, Flash plug-ins, pop-up dialog boxes and anything beyond control.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Thread and process &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;h2&gt;
  
  
  2.1 The difference between thread and process &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8umV73d9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uo0i6shu3o15lzuolfsu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8umV73d9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uo0i6shu3o15lzuolfsu.png" alt="Image description" width="807" height="1073"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2.2 Service Worker &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developers.google.com/web/ilt/pwa/introduction-to-service-worker"&gt;read more about service worker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A service worker is a script that runs on a independent thread, it  enable applications to &lt;strong&gt;control network requests&lt;/strong&gt; as it is a programmable network proxy which decides how requests from your page are handled., &lt;strong&gt;cache those requests to improve performance&lt;/strong&gt;, and &lt;strong&gt;provide offline access to cached content&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Act as the base for advanced features&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Service workers provide the starting point for features that make web applications work like native apps. Some of these features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Notifications API:&lt;/strong&gt; A way to display and interact with notifications using the operating system's native notification system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Push API:&lt;/strong&gt; An API that enables your app to subscribe to a push service and receive push messages. Push messages are delivered to a service worker, which can use the information in the message to update the local state or display a notification to the user. Because service workers run independently of the main app, they can receive and display notifications even when the browser is not running.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Background Sync API:&lt;/strong&gt; Lets you defer actions until the user has stable connectivity. This is useful to ensure that whatever the user wants to send is actually sent. This API also allows servers to push periodic updates to the app so the app can update when it's next online&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Channel Messaging API:&lt;/strong&gt; Lets web workers and service workers communicate with each other and with the host application. Examples of this API include new content notification and updates that require user interaction.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The service worker life cycle&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Registration&lt;/li&gt;
&lt;li&gt;Installation&lt;/li&gt;
&lt;li&gt;Activation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Y72KfMf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnvubrj7xjcrdq9k3ull.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Y72KfMf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnvubrj7xjcrdq9k3ull.png" alt="The service worker life cycle" width="702" height="685"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bitsofco.de/web-workers-vs-service-workers-vs-worklets/"&gt;&lt;strong&gt;Web workers&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kO8dWClq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkxqiser8gy55v1evd39.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kO8dWClq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkxqiser8gy55v1evd39.png" alt="Image description" width="566" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Web workers are the most general purpose type of worker. Unlike service workers and worklets as we will see below, they do not have a specific use case, other than the feature of being run separately to the main thread. As a result, web workers can be used to offload pretty much any heavy processing from the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The mechanism behind browser cache &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The process of browser caching:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The browser loads the resource for the first time, the server returns 200, and the browser downloads the resource file from the server, and caches the resource file and response header for comparison and use in the next load;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next time when client request to load resource, the time difference between the current time and the last return of 200 response is first compared. If it does not exceed the max-age set by cache-control, it will hit the cache and load resources locally. If the browser does not support HTTP1.1, use the expires header to determine whether it has expired;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the resource has expired, it means that the cached resource is may no longer valid, and a request with If-None-Match and If-Modified-Since section is sent to the server;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the server receives the request, it first validates whether the requested resource has been modified based on the Etag value. If the Etag value is the same, there is no modification 304 is returned; if E-tag value is inconsistent which means there is a change, and the new resource will be directly returned with a new Etag and status code 200. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the request received by the server does not have an Etag value, it will compare If-Modified-Since with the last modification time of the requested resource. If it has not changed, server will return 304; if they are inconsistent, server will return the response header with new last-modified, resource and return status code 200;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where are cache stored?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Service Worker: Note that if resource is not cached in service worker, it will call the fetch api to look for the cached resource in memory cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory Cache: memory cache will be erased once the tab is closed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Disk Cache: reading from disk cache is much slower than memory cache but it can store the resource for longer period.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why do we need browser cache?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For better loading speed of the web page&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;relieve the server's pressure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;avoid unnecessary network transmission&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Cookie, Session and LocalStorage &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.geeksforgeeks.org/difference-between-local-storage-session-storage-and-cookies/"&gt;read more about it here &lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aeq2f3-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6tk0xa8vast89f9s73v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aeq2f3-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6tk0xa8vast89f9s73v.png" alt="Image description" width="730" height="908"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cookie attributes:&lt;/strong&gt; We can set the cookie attributes in Server response header Set-cookie section&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;domain&lt;br&gt;
Includes the Domain attribute in the cookie to specify the domain for which the cookie is sent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;path&lt;br&gt;
Includes the Path attribute in the cookie to specify the path for which this cookie is sent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;secure&lt;br&gt;
Includes the Secure attribute in the cookie to set whether the cookie is transmitted only over an encrypted connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;httponly&lt;br&gt;
Includes the HttpOnly attribute in the cookie to set whether the cookie is exposed only through HTTP and HTTPS channels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;max-age&lt;br&gt;
Includes the Max-Age attribute in the cookie to specify the duration of the cookie.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;expires&lt;br&gt;
Includes the Expires attribute in the cookie to set the cookie expiration date.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;custom&lt;br&gt;
Includes custom attributes in the cookie to specify additional attributes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sameSite &lt;br&gt;
The SameSite attribute of the Set-Cookie HTTP response header allows you to declare if your cookie should be restricted to a first-party or same-site context.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Browser same origin policy &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Same-origin policy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The same-origin policy is a critical security mechanism that restricts how a document or script loaded by one origin can interact with a resource from another origin.&lt;/p&gt;

&lt;p&gt;It helps isolate potentially malicious documents, reducing possible attack vectors. For example, it prevents a malicious website on the Internet from running JS in a browser to read data from a third-party webmail service (which the user is signed into) or a company intranet (which is protected from direct access by the attacker by not having a public IP address) and relaying that data to the attacker.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Garbage Collection in Javascript V8 &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://medium.com/@_lrlna/garbage-collection-in-v8-an-illustrated-guide-d24a952ee3b8"&gt;click here to read more &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;V8 divides the heap into two parts: young space and old space. When you perform an operations that requires V8 to allocate memory, V8 allocates space in the first portion. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- In the new space&lt;/strong&gt;,&lt;br&gt;
 the memory space is divided into two parts, the From space and the To space. Of these two spaces, one space must be used and the other space is idle. The newly allocated objects will be put into the From space. When the From space is full, the new space Garbage Collector will start. The algorithm will check the surviving objects in the From space and copy them to the To space. If there are inactivated objects, they will be destroyed. When the copy is completed, swap the From space and To space, so that the GC is over.&lt;/p&gt;

&lt;p&gt;Once the objects ‘survive’ a few(two to be exact) scavenges in new space, they get promoted to old space, which gets garbage collected in a separate cycle when full.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- In the old space&lt;/strong&gt;,&lt;br&gt;
When the old space does not have enough space for object being moved from new space or the object in the space exceeds a certain limit. A mark and sweep garbage collection algorithm will be used.&lt;/p&gt;

&lt;p&gt;In this stage, all objects in the heap are traversed, and then the live objects are marked. After the marking is completed, all unmarked objects are destroyed. This will cause some performance problems in large memory. To solve this problem, in 2011, V8 switched from the stop-the-world flag to the incremental flag. During the incremental marking, the GC breaks the marking work into smaller modules, allowing the JS application logic to execute for a while between modules, so as not to cause the application to be blocked. But in 2018, GC technology has another major breakthrough, this technology is called concurrent marking. This technology allows GC to scan and mark objects while allowing JS to run.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. How to Keep user logged in credential &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;- Browser in-memory scenarios&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Token is actually a credential for accessing resources.&lt;/p&gt;

&lt;p&gt;Generally, after the user successfully logs in through the user name and password, the server digitally signs the login credentials, and the encrypted character string is used as the token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Store in the localStorage:&lt;/strong&gt;&lt;br&gt;
Web storage (localStorage/sessionStorage) can be accessed through the same domain provider through Javascript. This means that any JavaScript running on your website can access the web storage, so it is vulnerable to XSS attacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Store in Cookie&lt;/strong&gt;&lt;br&gt;
We can store it in a cookie and let it be sent automatically on every request, we can specify httponly to prevent it from being read by JavaScript or we can specify secure to ensure that the token is only transmitted under HTTPS to prevent being attacked by XSS, but the disadvantage is that it cannot cross domains and vulnerable to CSRF attacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recommended Solution&lt;/strong&gt;&lt;br&gt;
Auth0 recommends storing tokens in browser memory as the most secure option. Using Web Workers to handle the transmission and storage of tokens is the best way to protect the tokens, as Web Workers run in a separate global scope than the rest of the application. Use Auth0 SPA SDK whose default storage option is in-memory storage leveraging Web Workers.&lt;/p&gt;
&lt;h2&gt;
  
  
  8. What happened when Access-Control-Allow-Origin is * (wild card) &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;When Access-Control-Allow-Origin is *. The client side will not be able to carry the cookies in request even setting XMLHttpRequest's withCredentials: true.&lt;/p&gt;

&lt;p&gt;The withCredentials flag of XMLHttpRequest is set to true in order for the client to send cookies to the server. Access-Control-Allow-Credentials: true in the response is for the client to be able to receive cookies. Without these settings, the browser will not return the response content to the sender of the request.&lt;/p&gt;

&lt;p&gt;Therefore, for the client to carry cookies to the server, three conditions are required:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Access-Control-Allow-Origin cannot be *, it should be a specific domain name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server Access-Control-Allow-Credentials should be true&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;withCredentials=true of the client XMLHttpRequest&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  9. Communicate Across Browser Tabs &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://blog.bitsrc.io/4-ways-to-communicate-across-browser-tabs-in-realtime-e4f5f6cbedca"&gt;click here to read more&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Local Storage Events&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;window.localStorage.setItem("loggedIn", "true");&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The other Tabs which listen to the event will receive it, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;storage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storageArea&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loggedIn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Do something with event.newValue&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This event is not triggered for the Tab, which performs the storage set action.&lt;/li&gt;
&lt;li&gt;For a large chunk of data, this approach has adverse effects since LocalStorage is synchronous. And hence can block the main UI thread&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Broadcast Channel API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and Web Workers. One Tab can create and post to a channel as follows.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const channel = new BroadcastChannel('app-data');&lt;br&gt;
channel.postMessage(data);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And other Tabs can listen to channel as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;BroadcastChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Service Worker Post Message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using Service Workers, you can send a message like shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
 &lt;span class="na"&gt;broadcast&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the receiving Worker in the other Browser Tab can listen to the event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;boadcast&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;allClients&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;clients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;matchAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;allClients&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;broadcast&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method gives more control and is a reliable way of passing messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Window Post Message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the traditional ways of communicating across Browser Tabs, Popups, and Iframes is Window.postMessage() method. You can send the message as follows.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;targetWindow.postMessage(message, targetOrigin)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the target window can listen to the events, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One advantage of this method over the others is the support for cross-origin communication is possible.&lt;/p&gt;

&lt;p&gt;When we want to communicate between two tabs from different origin, we can use iframe as a bridge, post message to the iframe and listen to the message received from iframe.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Computer Network Concepts - The one and only guide you need</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Thu, 02 Dec 2021 21:47:59 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k</link>
      <guid>https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k</guid>
      <description>&lt;p&gt;"To become a Web Frontend Developer, we can never survive without Computer network knowledges. Frontend is never solely about Javascript or CSS. We work closely with network protocol and http request daily"&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. Common HTTP request that we will use&lt;br&gt;
2. Inheritable and non-inheritable properties in CSS&lt;br&gt;
3. HTTP respond Header &lt;br&gt;
4. Common HTTP respond status code &lt;br&gt;
5. Key differences between HTTP=1.0, HTTP=1.1, 2.0 &lt;br&gt;
6. What Happens When You Type URL into the browser &lt;br&gt;
7. How do we understand keep-alive connection&lt;br&gt;
8. What's the pros and cons for HTTP request&lt;br&gt;
9. HTTPS protocol &lt;br&gt;
10. Understand the OSI 7-layers model &lt;br&gt;
11.TCP and UDP &lt;br&gt;
12. Websocket&lt;br&gt;
13. Domain Name System (DNS)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki"&gt;React Concepts Part.1 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib"&gt;React Concepts Part.2 - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6"&gt;CSS Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch"&gt;Web Optimization Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni"&gt;Browser Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Common HTTP request that we will use &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/tags/ref_httpmethods.asp"&gt;&amp;gt; - What is HTTP? Refer to the W3school for full detail&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.&lt;br&gt;
HTTP works as a request-response protocol between a client and server.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Request Method&lt;/th&gt;
&lt;th&gt;Explaination&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET:&lt;/td&gt;
&lt;td&gt;GET is used to request data from a specified resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST：&lt;/td&gt;
&lt;td&gt;POST is used to send data to a server to create/update a resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT：&lt;/td&gt;
&lt;td&gt;PUT is used to send data to a server to create/update a resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE：&lt;/td&gt;
&lt;td&gt;The DELETE method deletes the specified resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HEAD：&lt;/td&gt;
&lt;td&gt;HEAD is almost identical to GET, but without the response body.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OPTIONS：&lt;/td&gt;
&lt;td&gt;The OPTIONS method describes the communication options for the target resource. (used for CORS preflight)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Difference between GET and POST : &lt;strong&gt;Noted that if URL length does not exceed 2,083 characters, then GET request will be good with almost every browser.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QaZjbIQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu56k3mu60kjbmsowwgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QaZjbIQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu56k3mu60kjbmsowwgi.png" alt="Image description" width="880" height="972"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. HTTP request Header &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/HTTP_header"&gt;Refer to this documentation&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;GET&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; 

&lt;span class="c1"&gt;// Http method, path of the resource, protocol &lt;/span&gt;

&lt;span class="nx"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;developer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mozilla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;org&lt;/span&gt;

&lt;span class="c1"&gt;// Domain of the current page who sends the request&lt;/span&gt;

&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Mozilla&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;5.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Macintosh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;Intel&lt;/span&gt; &lt;span class="nx"&gt;Mac&lt;/span&gt; &lt;span class="nx"&gt;OS&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt; &lt;span class="mf"&gt;10.9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;rv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;50.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;Gecko&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;20100101&lt;/span&gt; &lt;span class="nx"&gt;Firefox&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;50.0&lt;/span&gt;

&lt;span class="c1"&gt;// Browser'user Proxy &lt;/span&gt;

&lt;span class="nx"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;xhtml&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.9&lt;/span&gt;

&lt;span class="c1"&gt;//accepted content type for the browser&lt;/span&gt;

&lt;span class="nx"&gt;Accept&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;en&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;US&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;en&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt; 

&lt;span class="c1"&gt;//accept language for the browser&lt;/span&gt;

&lt;span class="nx"&gt;Accept&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;gzip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deflate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;br&lt;/span&gt; 

&lt;span class="c1"&gt;//accepted encoding method&lt;/span&gt;

&lt;span class="nx"&gt;Referer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//developer.mozilla.org/testpage.html&lt;/span&gt;

&lt;span class="c1"&gt;//the URL that make the request&lt;/span&gt;

&lt;span class="nx"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;keep&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;alive&lt;/span&gt; 

&lt;span class="c1"&gt;//the connection type between sever and client&lt;/span&gt;

&lt;span class="nx"&gt;If&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Modified&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Since&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Mon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="nx"&gt;Jul&lt;/span&gt; &lt;span class="mi"&gt;2016&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;04&lt;/span&gt; &lt;span class="nx"&gt;GMT&lt;/span&gt;
&lt;span class="nx"&gt;If&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;None&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c561c68d0ba92bbeb8b0fff2a9199f722e3a621a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;//for cache control&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. HTTP respond Header &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="nx"&gt;OK&lt;/span&gt;

&lt;span class="c1"&gt;// HTTP response status codes, short description&lt;/span&gt;

&lt;span class="nx"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="c1"&gt;// CORS&lt;/span&gt;

&lt;span class="nx"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Keep&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Alive&lt;/span&gt;

&lt;span class="c1"&gt;//connection type&lt;/span&gt;

&lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;utf&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;

&lt;span class="c1"&gt;// document type&lt;/span&gt;

&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Mon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="nx"&gt;Jul&lt;/span&gt; &lt;span class="mi"&gt;2016&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;06&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;GMT&lt;/span&gt;

&lt;span class="c1"&gt;//responding time&lt;/span&gt;

&lt;span class="nx"&gt;Etag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c561c68d0ba92bbeb8b0f612a9199f722e3a621a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;Last&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Modified&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Mon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="nx"&gt;Jul&lt;/span&gt; &lt;span class="mi"&gt;2016&lt;/span&gt; &lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;04&lt;/span&gt; &lt;span class="nx"&gt;GMT&lt;/span&gt;
&lt;span class="c1"&gt;//for cache control&lt;/span&gt;

&lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mykey&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;myvalue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;expires&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;Mon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Jul&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2017&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;06&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;GMT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;Max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;31449600&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sr"&gt;/; secur&lt;/span&gt;&lt;span class="err"&gt;e
&lt;/span&gt;
&lt;span class="c1"&gt;// set cookie expire time and other related information&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Common HTTP respond status code &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#successful_responses"&gt;Refer to this documentation for details&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Successful responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 200 OK&lt;/strong&gt;&lt;br&gt;
The request succeeded. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 204 No Content&lt;/strong&gt;&lt;br&gt;
There is no content to send for this request, but the headers may be useful. The user agent may update its cached headers for this resource with the new ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 206 Partial Content&lt;/strong&gt;&lt;br&gt;
This response code is used when the Range header is sent from the client to request only part of a resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redirection messages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 301 Moved Permanently&lt;/strong&gt;&lt;br&gt;
The URL of the requested resource has been changed permanently. The new URL is given in the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 302 temporary moved&lt;/strong&gt;&lt;br&gt;
A 301 redirect means that the page has permanently moved to a new location. A 302 redirect means that the move is only temporary. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 304 Not Modified&lt;/strong&gt;&lt;br&gt;
This is used for caching purposes. It tells the client that the response has not been modified, so the client can continue to use the same cached version of the response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client error responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 400 Bad Request&lt;/strong&gt;&lt;br&gt;
The server could not understand the request due to invalid syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 401 Unauthorized&lt;/strong&gt;&lt;br&gt;
Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 403 Forbidden&lt;/strong&gt;&lt;br&gt;
The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 404 Not Found&lt;/strong&gt;&lt;br&gt;
The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server error responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 500 Internal Server Error&lt;/strong&gt;&lt;br&gt;
The server has encountered a situation it does not know how to handle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 501 Not Implemented&lt;/strong&gt;&lt;br&gt;
The request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 502 Bad Gateway&lt;/strong&gt;&lt;br&gt;
This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- 503 Service Unavailable&lt;/strong&gt;&lt;br&gt;
The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Key differences between HTTP=1.0, HTTP=1.1, 2.0 &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://programmerall.com/article/6663649849/"&gt;Refer to the full article here&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;1. PersistentConnection&lt;/strong&gt;&lt;br&gt;
HTTP 1.0 stipulates that the browser and the server only maintain a short connection. The server immediately disconnects the TCP connection after processing the request.&lt;/p&gt;

&lt;p&gt;HTTP 1.1 supports Persistent Connection, and Long connection is used by default.For HTTP 1.1 long connections, new request headers need to be added to help achieve this. The value of the Connection request header is Keep-Alive, the client informs the server to keep the connection after returning the result of this request; the value of the Connection request header is close When, the client notifies the server to close the connection after returning the result of this request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Pipelining&lt;/strong&gt;&lt;br&gt;
　　 Request pipeline (Pipelining) processing, multiple HTTP requests and responses can be transmitted on a TCP connection, reducing the consumption and delay of establishing and closing connections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.host field:&lt;/strong&gt;&lt;br&gt;
　　 In HTTP1.0, it is believed that each server is bound to a unique IP address. Therefore, the URL in the request message does not convey the hostname. But with the development of virtual host technology, there can be multiple virtual hosts (Multi-homed Web Servers) on a physical server, and they share an IP address. With the host field HTTP1.1 can send request to different host on the same server&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.Bandwidth optimization:&lt;/strong&gt;&lt;br&gt;
　　 In HTTP/1.0, there are some phenomena of wasting bandwidth. For example, the client only needs a part of an object, but the server sends the entire object.&lt;/p&gt;

&lt;p&gt;　　 The range header field is introduced in the request message in HTTP/1.1,It allows to request only certain part of the resource. If the server correspondingly returns the content requested by the object, the response code is 206 (Partial Content), which can prevent the Cache from mistaking the response as a complete object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.Cache:&lt;/strong&gt;&lt;br&gt;
    HTTP/1.1 adds the Cache-Control header field which supports an extensible instruction subset: for example, the max-age instruction supports relative timestamp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP 2.0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Multiplexing:&lt;/strong&gt;&lt;br&gt;
　　 HTTP 2.0 uses multiplexing technology to process multiple requests concurrently on the same connection, and the number of concurrent requests is several orders of magnitude larger than HTTP 1.1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.data compression:&lt;/strong&gt;&lt;br&gt;
　　 HTTP 1.1 does not support header data compression, HTTP 2.0 uses HPACK algorithm to compress header data, so that the data volume is smaller and the transmission on the network will be faster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Server push:&lt;/strong&gt;&lt;br&gt;
　　 When we request data from a web server that supports HTTP2.0, the server will push some resources needed by the client to the client by the way, so that the client will not create a connection again and send a request to the server to obtain it. This method is very suitable for loading static resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.Binary framing:&lt;/strong&gt;&lt;br&gt;
　　 HTTP/2 adds a binary framing layer between the application layer (HTTP/2) and the transport layer (TCP or UDP).&lt;br&gt;
　　 Without changing the semantics, methods, status codes, URI and header fields of HTTP/1.x, it solves the performance limitations of HTTP 1.1, improves transmission performance, and achieves low latency and high throughput.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  6. What Happens When You Type in a URL into the browser&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. URL parsing:&lt;/strong&gt; The browser will extract the protocol and path of the requested resource.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Cache Validation:&lt;/strong&gt; Then browser will verify whether such requested resource has been cached and still valid. If yes it will return the cached result immediately or else it will send a new request to the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. DNS query:&lt;/strong&gt; The browser checks the browser cache for a DNS record to find the corresponding IP address. If not found, it will proceed to the OS cache followed by router cache. Lastly, ISP(Internet Service Provider)’s DNS server initiates a DNS query to find the IP address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Get the MAC address:&lt;/strong&gt; After the browser gets the IP address, the data transmission also needs to know the destination host's MAC address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. The browser initiates a TCP connection with the server.&lt;/strong&gt; It will require a three way handshake to establish the TCP connection between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. The browser sends an HTTP request to the webserver.&lt;/strong&gt; Once the TCP connection is established, the browser sends a HTTP request. Refer to the above section regarding HTTP request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Server handle the request and sends out an HTTP response.&lt;/strong&gt; &lt;br&gt;
The server response contains the web page you requested as well as the status code, compression type (Content-Encoding), how to cache the page (Cache-Control), any cookies to set, privacy information, etc. Refer to the above section regarding HTTP response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. The browser parse and paint the HTML content&lt;/strong&gt; The browser displays the HTML content in phases. First, it will render the DOM tree and CSSOM tree through the respective files and construct a render tree base on them. Once this is done the browser will layout the page through the render tree and use its api to paint the user interface on viewport.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. TCP four way handshake to stop the connection&lt;/strong&gt; Once the client thinks it has received everything it needs, it will start a four way handshake with the server to close the connection. Refer to the TCP section below for more information.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. How do we understand keep-alive connection &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In HTTP1.0, by default is to create a new connection between the client and the server for each request/response, and disconnect immediately after completion, which is a short connection.&lt;/p&gt;

&lt;p&gt;When the Keep-Alive mode is used, the Keep-Alive function keeps the client-to-server connection valid. When a subsequent request to the server occurs, the Keep-Alive function avoids establishing or re-establishing a connection, which is a long connection. The method of use is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The client sends the request to the server while adding the Connection field to the header. The server sends the Connection: Keep-Alive field back to the client. The client receives the Connection field and then Keep-Alive connection established successfully.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To close the connection, client side will send connection:closed inside of its request, server will respond and close the keep-alive connection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pros of long connection&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;less cpu and memory usage as there are less connections&lt;/li&gt;
&lt;li&gt;allows pipelining (refer to the above session about pipelining)&lt;/li&gt;
&lt;li&gt;let congestion control will be needed&lt;/li&gt;
&lt;li&gt;less delay in the subsequent request as the client side do not need to perform the three way handshake on every request.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cons of long connection&lt;/strong&gt;&lt;br&gt;
The long tcp connection may waste resources if it stays idle for long period.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8U-NwO_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f5tbq27geyw4gxqyc7a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8U-NwO_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f5tbq27geyw4gxqyc7a.png" alt="Image description" width="450" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. What's the pros and cons for HTTP request &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.Simple and fast: When a client requests a service from the server, it only needs to transmit the request method and path. Because the HTTP protocol is simple, the HTTP server request processing time will be very fast.&lt;/p&gt;

&lt;p&gt;2.connectionless: No connection means it limits each connection to only process one request. After the server has processed the client's request and received the client's response, it will disconnect. This method can save transmission time.&lt;/p&gt;

&lt;p&gt;3.Stateless: HTTP protocol is a stateless protocol, where the state refers to the context information of the communication process. The lack of state means that if the previous information is needed for subsequent processing, it must be retransmitted, which may result in an increase in the amount of data transmitted per connection. On the other hand, if the server does not need previous information, its response is faster.&lt;/p&gt;

&lt;p&gt;4.Flexible: HTTP allows the transmission of any type of data object. The type being transmitted is marked by Content-Type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Clear text transmission: The messages in the protocol are in text form, which is directly exposed to the outside world and is not safe. It may be manipulated by third party that's listening to the connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. HTTPS protocol &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.cloudflare.com/learning/ssl/what-is-https"&gt;&amp;gt; What is HTTPS?&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hypertext transfer protocol secure (HTTPS) is the secure version of HTTP, which is the primary protocol used to send data between a web browser and a website. HTTPS is encrypted in order to increase security of data transfer. This is particularly important when users transmit sensitive data, such as by logging into a bank account, email service, or health insurance provider.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What happens during a TLS handshake?&lt;/strong&gt;&lt;br&gt;
During the course of a TLS handshake, the client and server together will do the following:&lt;/p&gt;

&lt;p&gt;1.Specify which version of TLS (TLS 1.0, 1.2, 1.3, etc.) they will use&lt;/p&gt;

&lt;p&gt;2.Decide on which cipher suites (see below) they will use&lt;/p&gt;

&lt;p&gt;3.Authenticate the identity of the server via the server’s public &lt;br&gt;
key and the SSL certificate authority’s digital signature&lt;/p&gt;

&lt;p&gt;4.Generate session keys in order to use symmetric encryption after the handshake is complete&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mx98FtVw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1abd24mrte0y2ejk12t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mx98FtVw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1abd24mrte0y2ejk12t.png" alt="Image description" width="880" height="503"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.cloudflare.com/learning/ssl/what-happens-in-a-tls-handshake"&gt;Read more about https handshakes here &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Pros of HTTPS are as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use the HTTPS protocol to authenticate users and servers, and ensure that data is sent to the correct client and server;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using HTTPS protocol can carry out encrypted transmission, identity authentication, communication is more secure, prevent data from being stolen and modified during transmission, and ensure data security;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Cons of HTTPS are as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;HTTPS needs to perform encryption and decryption processing for both the server and the client, which consumes more server resources and the process is complicated&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The handshake phase of the HTTPS protocol is time-consuming, which increases the loading time of the page;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SSL certificate is expensive in cost (We talking about real money), the more powerful the certificate, the higher the cost;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The SSL certificate needs to be bind with an IP, and multiple domain names cannot be bind to the same IP.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  10. Understand the OSI 7-layers model &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eIbX3X29--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87r3uuw17636uuyfsl4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eIbX3X29--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87r3uuw17636uuyfsl4z.png" alt="Image description" width="500" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.imperva.com/learn/application-security/osi-model/"&gt;Click here for more information on each layer's functionality&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Open Systems Interconnection (OSI) model describes seven layers that computer systems use to communicate over a network. It was the first standard model for network communications, adopted by all major computer and telecommunication companies in the early 1980s&lt;/p&gt;

&lt;p&gt;The modern Internet is not based on OSI, but on the simpler TCP/IP model. However, the OSI 7-layer model is still widely used, as it helps visualize and communicate how networks operate, and helps isolate and troubleshoot networking problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  11.TCP and UDP &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lifesize.com/en/blog/tcp-vs-udp/"&gt;&lt;strong&gt;What is the Difference Between TCP and UDP?&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dnNXqpOZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70xka6sz1m60h2vs791n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dnNXqpOZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70xka6sz1m60h2vs791n.png" alt="Image description" width="513" height="971"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://accedian.com/blog/network-packet-loss-retransmissions-and-duplicate-acknowledgements/"&gt;&lt;strong&gt;TCP retransmission mechanism&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each byte of data sent in a TCP connection has an associated sequence number. This is indicated on the sequence number field of the TCP header.&lt;/p&gt;

&lt;p&gt;When the receiving socket detects an incoming segment of data, it uses the acknowledgement number in the TCP header to indicate receipt. After sending a packet of data, the sender will start a retransmission timer of variable length. If it does not receive an acknowledgment before the timer expires, the sender will assume the segment has been lost and will retransmit it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xwbqZFoE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdlaf1mxt66gxilsi5dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xwbqZFoE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdlaf1mxt66gxilsi5dy.png" alt="Image description" width="647" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.cs.dartmouth.edu/~campbell/cs60/congestion.pdf"&gt;&lt;strong&gt;TCP congestion control&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The TCP congestion control mechanism is consists following four mechanisms (refer to the link above to get more details):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow start&lt;/li&gt;
&lt;li&gt;Congestion avoidance&lt;/li&gt;
&lt;li&gt;Fast retransmission&lt;/li&gt;
&lt;li&gt;Fast recovery&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T_p7bzVa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbp4lgzvkksgqzp369o8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T_p7bzVa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbp4lgzvkksgqzp369o8.png" alt="Image description" width="880" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TCP 3-way handshake and close connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/tcp-3-way-handshake-process/"&gt;&lt;strong&gt;Three way handshake (refer to this link for details)&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GRs1XKOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8i0eik59qvdjd5btx3pa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GRs1XKOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8i0eik59qvdjd5btx3pa.png" alt="Image description" width="461" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 1 (SYN): In the first step, the client wants to establish a connection with a server, so it sends a segment with SYN(Synchronize Sequence Number) which informs the server that the client is likely to start communication and with what sequence number it starts segments with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2 (SYN + ACK): Server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of the segment it received and SYN signifies with what sequence number it is likely to start the segments with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3 (ACK): In the final part client acknowledges the response of the server and they both establish a reliable connection with which they will start the actual data transfer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/tcp-connection-termination/"&gt;&lt;strong&gt;Close connection (refer to this link for details)&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o1QpoMdL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1whcj8pkug6ip289ow35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o1QpoMdL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1whcj8pkug6ip289ow35.png" alt="Image description" width="424" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 1 (FIN From Client) – &lt;br&gt;
Suppose that the client application decides it wants to close the connection. (Note that the server could also choose to close the connection). This causes the client to send a TCP segment with the FIN bit set to 1 to the server and to enter the FIN_WAIT_1 state. While in the FIN_WAIT_1 state, the client waits for a TCP segment from the server with an acknowledgment (ACK).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2 (ACK From Server) – &lt;br&gt;
When the Server received the FIN bit segment from Sender (Client), Server Immediately sends acknowledgement (ACK) segment to the Sender (Client).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3 (Client waiting) – &lt;br&gt;
While in the FIN_WAIT_1 state, the client waits for a TCP segment from the server with an acknowledgment. When it receives this segment, the client enters the FIN_WAIT_2 state. While in the FIN_WAIT_2 state, the client waits for another segment from the server with the FIN bit set to 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 4 (FIN from Server) – &lt;br&gt;
The server sends the FIN bit segment to the Sender(Client) after some time when the Server sends the ACK segment (because of some closing process in the Server).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 5 (ACK from Client) – &lt;br&gt;
When the Client receives the FIN bit segment from the Server, the client acknowledges the server’s segment and enters the TIME_WAIT state. The TIME_WAIT state lets the client resend the final acknowledgment in case the ACK is lost. The time spent by clients in the TIME_WAIT state depends on their implementation, but their typical values are 30 seconds, 1 minute, and 2 minutes. After the wait, the connection formally closes and all resources on the client-side (including port numbers and buffer data) are released.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  12. Websocket &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;WebSocket mechanism&lt;/strong&gt;: The client notifies the WebSocket server of an event with all recipient IDs, and the server immediately notifies all active clients after receiving it, only those client whose ID is in the recipient list will handle the event from websocket server.&lt;/p&gt;

&lt;p&gt;The advantage of WebSocket are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Support two-way communication, real-time data streaming&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can send text or binary data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built on the TCP protocol, the implementation at the server side is relatively easy&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The data format is relatively lightweight, the resource required is small&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is no same origin restriction, the client can communicate with any server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It has good compatibility with HTTP protocol. The default ports used by websocket are also 80 and 443.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  13. Domain Name System (DNS) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;DNS is the abbreviation of Domain Name System, and it provides a host name to IP address conversion service. It is a distributed database composed of hierarchical DNS servers. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is an application layer protocol that defines how the host queries this distributed database. DNS makes our life easier for not having to remember the IP address.&lt;/p&gt;

&lt;p&gt;Function: Resolve the domain name into an IP address. The client sends a domain name query request to the DNS server (the DNS server has its own IP address), and the DNS server informs the client of the IP address of the Web server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Does DNS use TCP and UDP at the same time?&lt;/strong&gt;&lt;br&gt;
DNS takes port 53 and uses TCP and UDP protocols at the same time.&lt;br&gt;
(1) Use TCP protocol for zone transfer&lt;/p&gt;

&lt;p&gt;The secondary domain name server will regularly (usually 3 hours) to query the main domain name server to find out whether the data has changed. If there is a change, a zone transfer will be performed for data synchronization. The zone transfer uses TCP instead of UDP, because the amount of data transferred synchronously is much more than the amount of data in a request response.&lt;br&gt;
TCP is a reliable connection, which guarantees the accuracy of data.&lt;/p&gt;

&lt;p&gt;(2) UDP packets are smaller in size. UDP packets can't be greater than 512 bytes. So any application needs data to be transferred greater than 512 bytes require TCP in place. For example, DNS uses both TCP and UDP for valid reasons described below. UDP messages aren't larger than 512 Bytes and are truncated when greater than this size. DNS uses TCP for Zone transfer and UDP for name, and queries either regular (primary) or reverse. UDP can be used to exchange small information whereas TCP must be used to exchange information larger than 512 bytes. If a client doesn't get response from DNS, it must retransmit the data using TCP after 3-5 seconds of interval.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/dns-works-on-tcp-and-udp"&gt;click here to read more&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>CSS Concepts - The one and only guide you need</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Thu, 02 Dec 2021 13:50:08 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6</link>
      <guid>https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6</guid>
      <description>&lt;p&gt;"I'm just summarizing my CSS learning routine here, your follow will be my motivation to update. Hope it will help you to improve your understanding towards CSS as well. Feel Free to check the rest of my content as well(updates regularly)"&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. CSS selector Priority&lt;br&gt;
2. Inheritable and non-inheritable properties in CSS&lt;br&gt;
3. Difference between display's block, inline and inline-block &lt;br&gt;
4. How to hide the elements &lt;br&gt;
5. What's the difference between link and @import &lt;br&gt;
6. Transition and Animation &lt;br&gt;
7. Pseudo class and pseudo element &lt;br&gt;
8. How to understand &lt;code&gt;requestAnimationframe&lt;/code&gt; &lt;br&gt;
9. Content box vs Border box &lt;br&gt;
10. Why Moving Elements With Translate() Is Better Than position:absolute Top/left &lt;br&gt;
11.How do you understand CSS Sprites &lt;br&gt;
12.What are the measures to optimize CSS performance&lt;br&gt;
13.Why are we using css preprocessor and postprocessor&lt;br&gt;
14.How to determine whether an element has appeared in our viewport &lt;br&gt;
15.Understand the media query &lt;br&gt;
16.When does z-index not work&lt;br&gt;
17.CSS layout unit&lt;br&gt;
18.How to achieve a two column layout&lt;br&gt;
19.How to achieve a three column layout&lt;br&gt;
20.Horizontal and vertical centering&lt;br&gt;
21.Understanding Flex box model&lt;br&gt;
22.Responsive Design&lt;br&gt;
23.Position and Floating&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki"&gt;React Concepts Part.1 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib"&gt;React Concepts Part.2 - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni"&gt;Browser Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch"&gt;Web Optimization Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k"&gt;Computer Network Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. CSS selector Priority &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The CSS selector priority is classified in to few different categories, each carries a different weightage. We can calculate the actual priority by summing up all the selectors' weightage. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Selector&lt;/th&gt;
&lt;th&gt;Syntax&lt;/th&gt;
&lt;th&gt;Weightage&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inline Style&lt;/td&gt;
&lt;td&gt;&lt;code&gt;style="color&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Id selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;#id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;class selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.classname&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;attributes selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;d[ref=“abc”]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pseudo class selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;li:first-child&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTML tag selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;div&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pseudo element selector&lt;/td&gt;
&lt;td&gt;&lt;code&gt;li:after&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
In fact, if you use the !important rule, it will override ALL previous styling rules for that specific property on that element!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p {
  background-color: red !important;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
If two styles applied have the same priority, the latter one will be selected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Inheritable and non-inheritable properties in CSS &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. non-inheritable properties&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Display&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Text attributes :&lt;/strong&gt; vertical-align、text-decoration、 text-shadow、 white-space、 unicode-bidi&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Box model attributes:&lt;/strong&gt; width、height、margin、border、padding&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Background attributes:&lt;/strong&gt; background、background-color、background-image、background-repeat、background-position、background-attachment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Position attributes:&lt;/strong&gt; float、clear、position、top、right、bottom、left、min-width、min-height、max-width、max-height、overflow、clip、z-index&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Generated content attributes:&lt;/strong&gt; content、counter-reset、counter-increment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Outline style attributes: **outline-style、outline-width、outline-color、outline&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Page style attributes：&lt;/strong&gt;size、page-break-before、page-break-after&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Audio style attributes：&lt;/strong&gt;pause-before、pause-after、pause、cue-before、cue-after、cue、play-during&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. inheritable properties&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;font attributes&lt;/strong&gt;: font-family、font-weight、font-size、font-style&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;text attributes&lt;/strong&gt;: text-indent、text-align、line-height、&lt;br&gt;
word-spacing、letter-spacing、text-transform、color.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Visibility&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;List Layout attributes&lt;/strong&gt;: list-style&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;cursor&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Difference between display's block, inline and inline-block &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;block:&lt;/strong&gt; block starts on a NEW line and takes up the full width available. So that means block elements will occupy the entire width of its parent element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;inline:&lt;/strong&gt; displays the element inline or on the same line. In other words, inline elements do NOT start on a new line and only takes up as much width as its content. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;inline-block:&lt;/strong&gt; It’s essentially the same thing as inline, except that you can set height and width values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. How to hide the elements &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;display: none:&lt;/strong&gt; such element will not be rendered, thus it will not take up any space in the page, and the event binded to such element will not be triggered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;visibility: hidden：&lt;/strong&gt; the element will still hold in the page and it will respond to the events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;opacity: 0：&lt;/strong&gt; set the transparency of the element to 0, behaves the same as visibility: hidden&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;position: absolute:&lt;/strong&gt; use absolute position to move the element outside of the viewport.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;z-index:negative value:&lt;/strong&gt; use other elements to fully cover it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;transform: scale(0,0)：&lt;/strong&gt; scale the size of the element to 0,0 such element will still exist in the page, however it will not listen to the events binded.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. What's the difference between link and &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt;  &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;These are both used to reference external CSS files. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;link is an html tag which can load more than just css files. &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt; on the other hand can only load css.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;link can load the css at the same time as the webpage is loading, &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt; can only load css after the webpage has been completed loaded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Javascript can mutate the link attributes by accessing the DOM, &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt; does not support such way.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Transition and Animation  &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;transitions:&lt;/strong&gt; For a transition to take place, an element must have a change in state, and different styles must be identified for each state. The easiest way for determining styles for different states is by using the &lt;code&gt;:hover, :focus, :active, and :target pseudo-classes&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;animations:&lt;/strong&gt; when more control is required, transitions need to have multiple states. In return, this is why we need animation. It does not need to be triggered by any events and the animation can be looped. We can set multiple key frame points by using &lt;a class="mentioned-user" href="https://dev.to/keyframe"&gt;@keyframe&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. Pseudo class and pseudo element &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Pseudo-classes act as simple selectors in a sequence of selectors and thereby classify elements on non-presentational characteristics, pseudo-elements create new virtual elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. How to understand &lt;code&gt;requestAnimationframe&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;There used to be just one way to do a timed loop in JavaScript: setInterval(). If you needed to repeat something pretty fast (but not as-fast-as-absolutely-possible like a for loop), you’d use that. For the purposes of animation, the goal is sixty “frames” per second to appear smooth, so you’d run a loop like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setInterval(function() {
  // animiate something
}, 1000/60);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame" rel="noopener noreferrer"&gt;Now there is a better way by using &lt;code&gt;requestAnimationframe&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.&lt;/p&gt;

&lt;p&gt;Your callback routine must itself call requestAnimationFrame() again if you want to animate another frame at the next repaint. requestAnimationFrame() is 1 shot. The number of callbacks is usually 60 times per second.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*&lt;em&gt;What are the advantages *&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save the CPU resources : With the animation implemented by SetTinterval, when the page is hidden or minimized, SetTinterval still performs animation tasks in the background. Since the page is in an invisible or unavailable state at this time, refreshing the animation is meaningless and completely wastes CPU resources. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The RequestAnimationFrame is completely different. When the page processing is not activated, the screen refresh task of the page will also be suspended by the system. Therefore, the RequestAnimationFrame that follows the system will also stop rendering. When the page is activated, the animation will stay from the last time. Continuing execution wherever it is, effectively saving CPU overhead.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Throttling : In high-frequency events (resize, scroll, etc.), in order to prevent multiple function executions in one refresh interval, RequestAnimationFrame can ensure that the function is executed only once in each refresh interval, so as to ensure fluency , It can also save the cost of function execution better. It is meaningless when the function is executed multiple times within a refresh interval, because most displays refresh every 16.7ms, and multiple draws will not be reflected on the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/19764018/controlling-fps-with-requestanimationframe" rel="noopener noreferrer"&gt;How to throttle requestAnimationFrame to a specific frame rate&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce DOM operations: requestAnimationFrame will collect all DOM operations in each frame and complete it in one repaint&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why we shouldn't use &lt;code&gt;setTimeout&lt;/code&gt; to control animation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Since setTimeout is a asynchronous task, it will only be executed when all the synchronous task are done, there is always a time delay.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Its fix time period will not exactly match the screen refreshing rate which leads to loss of frame.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. Content box vs Border box &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;content-box:&lt;/strong&gt; The width &amp;amp; height of the element only include the content. In other words, the border, padding and margin aren’t part of the width or height. This is the default value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;border-box:&lt;/strong&gt; The padding and border are included in the width and height.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdvhlqa84exhai3zh1ms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdvhlqa84exhai3zh1ms.png" alt="Boxmodel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Why Moving Elements With Translate() Is Better Than position:absolute Top/left &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Translate is a method in transform property. Changing the transform or opacity will not trigger the browser to reflow and repaint, it will only trigger compositions. &lt;/p&gt;

&lt;p&gt;However, changing the absolute positioning will trigger a re-layout, which will trigger re-paint and composititon. The transform ask the browser to create a GPU layer for the element, but changing the absolute positioning will use the CPU. Therefore translate() is more efficient and can shorten the drawing time of smooth animation. When translate changes its position, the element still occupies its original space, and this will not happen with absolute positioning.&lt;/p&gt;

&lt;h3&gt;
  
  
  11.How do you understand CSS Sprites &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;CSS Sprites are a means of combining multiple images into a single image file for use on a website, to help with performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can use background-image,background-repeat,background-position&lt;br&gt;
to located the image. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It can minimize the http request that client side has to make for retrieving image resources from the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Combining multiple image into one will also reduce the image size&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Require precise measurements on each image's size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When some part of the image has changed, we have to edit the combined image.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  12.What are the measures to optimize CSS performance &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Loading performance:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;compress CSS file to reduce file size&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;use single css property instead of shorthand property&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;do not use &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt;, use link instead&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Selectors:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use key selectors instead of Descendant combinator as the latter will iterate through all its descendants on the tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When using ID selector, don't add unnecessary selectors&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;do not use * selector&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;use class selector instead of HTML tag selector&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;avoid repeatly assigning styles to elements, make use of the inheritable properties.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rendering performance:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;use float and position carefully as they consume a lot of resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;avoid frequent rerendering&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;use CSS spirte efficiently&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  13.Why are we using css preprocessor and postprocessor&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;css preprocessor: less, sass, stylus &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;postprocessor: postCss&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reason for use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To make a clear CSS structure, easy to expand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can easily prevent different browers' syntax difference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiple inheritance can be easily achieved.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Perfectly compatible with CSS code and can be applied to old projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  14.How to determine whether an element has appeared in our viewport &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;window.innerHeight&lt;/code&gt; is the viewport height &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;document.body.scrollTop&lt;/code&gt; || document.documentElement.scrollTop is the distance that brower has scrolled.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;imgs.offsetTop&lt;/code&gt; is the distance of the element's top to the top of the document&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If img.offsetTop &amp;lt; window.innerHeight + document.body.scrollTop` || document.documentElement.scrollTop that means the img has already shown up in the viewport.&lt;/p&gt;

&lt;h3&gt;
  
  
  15.Understand the media query &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Media queries in CSS3 extended the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Media queries can be used to check many things, such as:&lt;/p&gt;

&lt;p&gt;width and height of the viewport&lt;br&gt;
width and height of the device&lt;br&gt;
orientation (is the tablet/phone in landscape or portrait mode?)&lt;br&gt;
resolution&lt;br&gt;
Using media queries are a popular technique for delivering a tailored style sheet to desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).&lt;/p&gt;

&lt;h3&gt;
  
  
  16.when does z-index not work &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The z-index property only works on elements with a position value other than static (e.g. position: absolute;, position: relative;, or position: fixed).&lt;/p&gt;

&lt;p&gt;It will not be functioning properly if :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Parent container's position is relative&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the element with z-index has also been set with float&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  17.CSS layout unit &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://getflywheel.com/layout/choose-css-unit-create-better-site-layouts-how-to/" rel="noopener noreferrer"&gt;CSS units can be separated in the following categories:&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Absolute units&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Font relative units&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Viewport relative units&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Absolute units:&lt;/p&gt;

&lt;p&gt;Some units depend on certain absolute values and are not affected by any screen size or fonts. These units display may vary depending on different screen resolutions, as they depend on DPI (dots per inch) of screens.&lt;/p&gt;

&lt;p&gt;These units are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;px (pixels)&lt;/li&gt;
&lt;li&gt;in (inches)&lt;/li&gt;
&lt;li&gt;cm (centimeter)&lt;/li&gt;
&lt;li&gt;mm (millimeter)&lt;/li&gt;
&lt;li&gt;pc (picas)&lt;/li&gt;
&lt;li&gt;pt (points)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Font relative units:&lt;/p&gt;

&lt;p&gt;There are some units which depend on the font size or font family of the document or its parent level elements. This includes units like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;em&lt;/li&gt;
&lt;li&gt;rem&lt;/li&gt;
&lt;li&gt;ex&lt;/li&gt;
&lt;li&gt;ch&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Viewport relative units:&lt;/p&gt;

&lt;p&gt;There are a few units that depend on the viewport height and width size, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;vh (viewport height)&lt;/li&gt;
&lt;li&gt;vw (viewport width)&lt;/li&gt;
&lt;li&gt;vmin (viewport minimum)&lt;/li&gt;
&lt;li&gt;vmax (viewport maximum)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Percentage(%) unit does not belong to any category above.&lt;/p&gt;

&lt;h3&gt;
  
  
  18.How to achieve a two column layout &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Two column layout usually refers to a layout which has a fix width left column and auto fill right column&lt;/p&gt;

&lt;p&gt;1.Float left element to the left, set the width to 200px, set the margin-left of the right element to the width of left element. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;.outer {&lt;br&gt;
  height: 100px;&lt;br&gt;
}&lt;br&gt;
.left {&lt;br&gt;
  float: left;&lt;br&gt;
  width: 200px;&lt;br&gt;
  background: tomato;&lt;br&gt;
}&lt;br&gt;
.right {&lt;br&gt;
  margin-left: 200px;&lt;br&gt;
  width: auto;&lt;br&gt;
  background: gold;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2.make use of the absolute position, before that we have to set the position property of the parent container to anything other than static. Then set the left element's position to absolute with 200px width. Follow by right element's margin-left to 200px.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.outer {&lt;br&gt;
  position: relative;&lt;br&gt;
  height: 100px;&lt;br&gt;
}&lt;br&gt;
.left {&lt;br&gt;
  position: absolute;&lt;br&gt;
  width: 200px;&lt;br&gt;
  height: 100px;&lt;br&gt;
  background: tomato;&lt;br&gt;
}&lt;br&gt;
.right {&lt;br&gt;
  margin-left: 200px;&lt;br&gt;
  background: gold;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.Use the flex layout, set left element width to 200px, set right element flex property to 1&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.outer {&lt;br&gt;
  display: flex;&lt;br&gt;
  height: 100px;&lt;br&gt;
}&lt;br&gt;
.left {&lt;br&gt;
  width: 200px;&lt;br&gt;
  background: tomato;&lt;br&gt;
}&lt;br&gt;
.right {&lt;br&gt;
  flex: 1;&lt;br&gt;
  background: gold;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  19.How to achieve a three column layout &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Three column layout refers to having the left and right element with fixed width, middle element will auto fill the gap.&lt;/p&gt;

&lt;p&gt;1.Use absolute position, set left element to a certain width. Set right element's top and right attribute to 0 so it will stick to the right side of the container. Lastly set the margin-left and margin-right of the centered element to the respective width of left and right element.&lt;/p&gt;

&lt;p&gt;`.outer {&lt;br&gt;
  position: relative;&lt;br&gt;
  height: 100px;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.left {&lt;br&gt;
  position: absolute;&lt;br&gt;
  width: 100px;&lt;br&gt;
  height: 100px;&lt;br&gt;
  background: tomato;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.right {&lt;br&gt;
  position: absolute;&lt;br&gt;
  top: 0;&lt;br&gt;
  right: 0;&lt;br&gt;
  width: 200px;&lt;br&gt;
  height: 100px;&lt;br&gt;
  background: gold;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.center {&lt;br&gt;
  margin-left: 100px;&lt;br&gt;
  margin-right: 200px;&lt;br&gt;
  height: 100px;&lt;br&gt;
  background: lightgreen;&lt;br&gt;
}`&lt;/p&gt;

&lt;p&gt;2.Use flex layout, set left and right element with a fixed width, let centered element's flex: 1&lt;/p&gt;

&lt;p&gt;`.outer {&lt;br&gt;
  display: flex;&lt;br&gt;
  height: 100px;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.left {&lt;br&gt;
  width: 100px;&lt;br&gt;
  background: tomato;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.right {&lt;br&gt;
  width: 100px;&lt;br&gt;
  background: gold;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;.center {&lt;br&gt;
  flex: 1;&lt;br&gt;
  background: lightgreen;&lt;br&gt;
}`&lt;/p&gt;

&lt;h3&gt;
  
  
  20.Horizontal and vertical centering &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;use the absolute position, set left and top to 50% so that the left corner of the element will appear in the center of the element. Use translate to adjust the center point of the
child element to match the parent's.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;.parent{position: relative;} &lt;br&gt;
.child {position: absolute;    &lt;br&gt;
        left: 50%;    &lt;br&gt;
        top: 50%;    &lt;br&gt;
        transform: translate(-50%,-50%);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use flex layout, set align-items:center and justify-content:center&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;.parent {&lt;br&gt;
    display: flex;&lt;br&gt;
    justify-content:center;&lt;br&gt;
    align-items:center;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  21.Understanding Flex box model &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Flex is a new value added to the CSS display property. Along with inline-flex it causes the element that it applies to in order to become a flex container, and the element's children to each become a flex item. The items then participate in flex layout, and all of the properties defined in the CSS Flexible Box Layout Module may be applied.&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Flex" rel="noopener noreferrer"&gt;Get your hands on experience in MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2q1uj2wjx6ay1mexipyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2q1uj2wjx6ay1mexipyw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk4ejkuvith4lunfvtzf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk4ejkuvith4lunfvtzf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  22.Responsive Design &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Responsive Web design is a website that can be compatible with multiple terminals, instead of making a specific version for each terminal.&lt;/p&gt;

&lt;p&gt;The basic principle is to use media query (&lt;a class="mentioned-user" href="https://dev.to/media"&gt;@media&lt;/a&gt;) query to detect different device screen sizes for processing.&lt;/p&gt;

&lt;p&gt;About compatibility: The page header must have the viewport declared by meta.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;meta name="’viewport’" content="”width=device-width," initial-scale="1." maximum-scale="1,user-scalable=no”"/&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  23.Position and Floating &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context" rel="noopener noreferrer"&gt;Read the BFC documentation to continue&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A block formatting context is a part of a visual CSS rendering of a web page. It's the region in which the layout of block boxes occurs and in which floats interact with other elements.&lt;br&gt;
Formatting contexts affect layout, but typically, we create a new block formatting context for the positioning and clearing floats rather than changing the layout, because an element that establishes a new block formatting context will:&lt;/p&gt;

&lt;p&gt;contain internal floats.&lt;br&gt;
exclude external floats.&lt;br&gt;
suppress margin collapsing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In progress...&lt;/p&gt;

&lt;p&gt;In progress...&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React Concepts Part.2 - The one and only guide you need</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Wed, 01 Dec 2021 16:03:21 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib</link>
      <guid>https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib</guid>
      <description>&lt;p&gt;&lt;em&gt;"I'm just summarizing my React learning routine here and this is the second part,your follow will be my motivation to update. Hope it will help you to improve your understanding towards React as well. Noted that the React version discussed here starts from 16.8 onwards. Concept such as HOC and class components are not included. (updates regularly)"&lt;/em&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. What are the Lifecycle of Components?&lt;br&gt;
2. React Hooks in Function components&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;2.1 The differences between Function components and Class components&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.2.Why are we using array instead of object in &lt;code&gt;useState()&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.3 What problems have been solved by Hooks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.4 Rule of Hooks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.5 Difference between useEffect and useLayoutEffect&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;2.6 Relationship between life cycle and hooks&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3. Difference between React.map and JS.map&lt;br&gt;
4. Why are we using JSX&lt;br&gt;
5. Communication between Components&lt;br&gt;
6. React Routers (In progress)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;6.1 Client router concept&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;6.2 Switch between different routes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;6.3 Redirection in React Router&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7. Redux (Coming soon)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki"&gt;React Concepts Part.1 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6"&gt;CSS Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k"&gt;Computer Network Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch"&gt;Web Optimization Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni"&gt;Browser Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What are the Lifecycle of Components? &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Each component in React has a lifecycle which you can monitor and manipulate during its three main phases.&lt;br&gt;
The three phases are: Mounting, Updating, and Unmounting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Mounting means putting elements into the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next phase in the lifecycle is when a component is updated. A component is updated whenever there is a change in the component's state or props.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4dhywfr73ze9fi3dft8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4dhywfr73ze9fi3dft8.png" alt="React Life Cycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. React Hooks in Function components &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;2.1 The differences between Function components and Class components&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Before hooks was introduced)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inital Class components need to extends from &lt;code&gt;React.Component&lt;/code&gt;, function components do not need to do so 
&lt;/li&gt;
&lt;li&gt;Class components can access life cycle methods, but function components cannot. 
&lt;/li&gt;
&lt;li&gt;Class components can access the this in the instantiated object. 
 &lt;/li&gt;
&lt;li&gt;Function components can't define and manage the state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key concept of designing React component is to treat it as function, a function that inputs data and outputs UI. It convert our declarative code into imperative DOM. Data and renders should be bind together. Function Component has achieved this in version 16.8 with the help of Hooks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.2 Why are we using array instead of object in &lt;code&gt;useState()&lt;/code&gt;&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;By using array destructuring, we can give any name to the variables in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we are using object destructuring, we have to use the identical name to the retrieved object's property name.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.3 What problems have been solved by Hooks&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The use of hooks reduces the number of concepts needed in the development of React applications, hooks offer us homogeneity in the ecosystem. And React lifecycle has been greatly simplified.&lt;/p&gt;

&lt;p&gt;Hook extracts state logic from components so that these logics can be tested and reused separately. Hook allows us to reuse state logic without modifying the component structure. This makes it easier to share Hooks between components or within the community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.4 Rule of Hooks&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Only Call Hooks at the Top Level, Do not call Hooks in loops, conditions, or nested functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only Call Hooks from React Functions, Do not Call Hook in Javascript's functional component.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.5 Difference between useEffect and useLayoutEffect&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; will be called asynchronously during rendering that runs after react has rendered all the components to&lt;br&gt;
ensures that effect callback does not block browser painting. It change the DOM after rendering which results to screen to blinking.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useLayoutEffect runs synchronously immediately after React has performed all DOM mutations and then proceed to rendering, hence avoid using it with heavy calculation callbacks which may block the UI display. It can be useful if you need to make DOM measurements such as the scroll position or DOM mutations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.6 Relationship between life cycle and hooks&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Class Components&lt;/th&gt;
&lt;th&gt;Hooks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;getDerivedStateFromProps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;useState&lt;/code&gt;'s update function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;shouldComponentUpdate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;useMemo&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;componentDidMount&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;useEffect&lt;/code&gt; with empty dependency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;componentDidUpdate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;useEffect&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;componentWillUnmount&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;useEffect&lt;/code&gt;'s return function&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  3. Difference between React.map and JS.map &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The map method in Javascript will not process the null and undefined values. However React.child.mao will handle them in some situation.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Why are we using JSX &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return React.createElement(
        'div',
        null, 
        `Hello ${this.props.toWhat}`
      );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSX is a syntax extension of JavaScript for &lt;code&gt;React.createElement()&lt;/code&gt; method. Using XML will have a better readability.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Communication between Components &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;From parent to child components: Use props to pass data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From child to parent components: Use props to pass the callback function and let the child component to call the function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use context or Redux to handle global states cross levels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Event Publisher/Subscriber: The publisher and subscriber are unaware of each other. All the communication between them is taken through events which are emitted from the publisher and notifies subscriber. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  6. React routers (In progress...)&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactrouter.com/docs/en/v6/getting-started/concepts" rel="noopener noreferrer"&gt;Learn more about routers here!!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.1 React router concept&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In React, routers help create and navigate between the different URLs that make up your web application. They allow your user to move between the components of your app while preserving user state, and can provide unique URLs for these components to make them more shareable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does browser router works?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Based on HTML5 history routing: To change the url, we can use history.pushState and replaceState to push the URL onto the history stack, and at the same time, we can apply APIs such as history.go(). Monitoring url changes can be triggered by custom events&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hash-based routing: Listening to &lt;code&gt;hashChange&lt;/code&gt; event. We can directly change hash by assign variable to location.hash&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Location Hash&lt;/p&gt;

&lt;p&gt;Hashes in URLs indicate a scroll position on the current page. Before the window.history.pushState API was introduced, web developers did client side routing exclusively with the hash portion of the URL, it was the only part we could manipulate without making a new request to the server. However, today we can use it for its designed purpose.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React router concept&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before React Router can do anything, it has to be able to subscribe to changes in the browser history stack.&lt;/p&gt;

&lt;p&gt;Through the maintained list by react router, each time the URL changes, the corresponding Component is matched and rendered through the configured routing path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Use Cases"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HashRouter:&lt;/strong&gt; When we have small client side applications which doesn't need backend we can use HashRouter because when we use hashes in the URL/location bar browser doesn't make a server request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BrowserRouter:&lt;/strong&gt; When we have big production-ready applications which serve backend, it is recommended to use .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.2 Switch between different routes&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt; Component&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;&amp;lt;Switch&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;lt;Route&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;code&gt;&amp;lt;Link&amp;gt;&lt;/code&gt;、 &lt;code&gt;&amp;lt;NavLink&amp;gt;&lt;/code&gt;、&lt;code&gt;&amp;lt;Redirect&amp;gt;&lt;/code&gt; components&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6.3 Redirection in React Router&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;&amp;lt;Redirect&amp;gt;&lt;/code&gt; component&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. Redux &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Coming soon... &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Concepts Part.1 - The one and only guide you need</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Wed, 01 Dec 2021 07:05:14 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki</link>
      <guid>https://dev.to/weifengnusceg/my-frontend-developer-learning-route-react-in-progress-10ki</guid>
      <description>&lt;p&gt;&lt;em&gt;"I'm just summarizing my React learning routine here, your follow will be my motivation to update. Hope it will help you to improve your understanding towards React as well. Noted that the React version discussed here starts from 16.8 onwards. Concept such as HOC and class components are not included. (updates regularly)"&lt;/em&gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;p&gt;1. Why do we choose to use React?&lt;br&gt;
2. React Synthetic Events&lt;br&gt;
3. Why should we use hooks in React?&lt;br&gt;
4.How do we understand React-Fiber and what problems has it solved?&lt;br&gt;
5. What's the difference between Component and Element&lt;br&gt;
6.When will component rerender in react?&lt;br&gt;
7. What will happen during re-rendering&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
7.1 What is React key?
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8. Stateless Component&lt;br&gt;
9. How to get the DOM element in React 16.8? &lt;br&gt;
10. React Portals &lt;br&gt;
11. How to avoid unnecessary re-renders in React 16.8? &lt;br&gt;
12. The mechanism behind React context &lt;br&gt;
13. Uncontrolled Components &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Contents
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga"&gt;HTML - The one and only guide you need (in progress)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/frontend-developer-learning-routine-react-concepts-p2-16ib"&gt;React Concepts Part.2 - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/css-concepts-the-one-and-only-guide-you-need-bb6"&gt;CSS Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/computer-network-concepts-the-one-and-only-guide-you-need-5d2k"&gt;Computer Network Concepts - The one and only guide you need&lt;/a&gt; &lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/web-optimization-concepts-the-one-and-only-guide-you-need-in-progress-bch"&gt;Web Optimization Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/weifengnusceg/browser-concepts-the-one-and-only-guide-you-need-3bni"&gt;Browser Concepts - The one and only guide you need&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Why do we choose to use React? &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;- It allows server-side rendering.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Server-side rendering (SSR) is an application’s ability to convert HTML files on the server into a fully rendered HTML page for the client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A server-side rendered application enables pages to load faster, improving the user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search engines can easily index and crawl content because the content can be rendered before the page is loaded, which is ideal for SEO&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rendering server-side helps efficiently load webpages for users with slow internet connection or outdated devices.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Rendering static HTML from server-side is efficient, but for complex applications with frequent server requests and full page reloads can increase load times due to the bottleneck of the server performance. It can be very costly and pressuring the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server-side rendering may not be compatible with third-party JavaScript code&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;- It uses the virtual DOM instead of the real DOM.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;React use &lt;em&gt;batch update mechanism&lt;/em&gt; to update the real DOM. Hence, leading to increased performance. This means that updates to the real DOM are sent in batches, instead of sending updates for every single change in state as Frequent DOM manipulations are expensive and performance heavy.&lt;/p&gt;

&lt;p&gt;React virtual DOM is updated with the state changes. The previous and current version of virtual DOM is compared through an efficient diff algorithm. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- It follows uni-directional data flow or data binding.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Uni-directional data flow gives you the control over how data flows throughout your app. The data flows in a single direction from parent to child making it much more predictablef for tracing and debugging&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- It is component based and extensive.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Component based provides React with better code maintenance and growth as each component in React has their own internal logic, which is easy to manipulate.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. React Synthetic Events&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Synthetic events are delegated to document instead of the real DOM node. Therefore native events are triggered first and the events bubble up to doucment, after which the synthetic events are triggerd.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It provides better cross-browser compatibility as it provides a uniform api and consistent behavior on top level.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It provides better performance as for the native broswer events, the browser will create a new event object for the listener and bind it to the node. If we have multiple listener, multiple objects will be created and require a lot of resources from the memory. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, the synthetic events objects are pooled and managed together. SyntheticEvent object will be reused and all properties will be nullified after the event handler has been called. &lt;/p&gt;

&lt;p&gt;To stop the native broswer event from bubbling, we should use event.preventDefault()&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. Why should we use hooks in React?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Hooks are easier to work with and to test.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code looks cleaner, easier to read. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;4.How do we understand React-Fiber and what problems has it solved?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In previous React Engines' rendering process (V15), it will recursively compare the virtual DOM's change and update to the real DOM in one shot. This process wouldn't stop in any case. The events triggered by users such as inputting text will not be respond as browser's resources are occupied which cause lagging and drop in frame.&lt;/p&gt;

&lt;p&gt;To improve this, Fiber now allows React to pause, resume, and restart the work on a component. It has a priority-based update system which allows the React to fine tune the renderer process.&lt;/p&gt;

&lt;p&gt;However fiber is not equal to thread. It represents a mechanism to give up the execution rights of the CPU so that the CPU can perform other operations during this time. The rendering process can be interrupted and control can be returned to the browser, giving way to high-priority tasks such as user triggered events, and rendering can be resumed when the browser is idle.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;5. What's the difference between Component and Element&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Element: An element is a plain object describing a component instance or DOM node and its desired properties. It is a way to tell React what you want to see on the screen. They are not the actual instances. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Component: Component encapsulate element trees, but generally it can be deemed as a function which takes props as arguments and return a Element tree.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(Never create a instance, React will help us do it)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;6.When will component rerender in react?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;React components re-render whenever there is a change in their state or props. &lt;/p&gt;

&lt;p&gt;A simple update of the state (e.g using &lt;code&gt;setState()&lt;/code&gt;), causes the component and all its child components to be re-rendered automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;7. What will happen during re-rendering&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Recursively Compare the previous VDOM with the current VDOM by DIFF algorithm. (using DFS to traverse every node, put the difference in an object if there is any.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React's basic concept for processing renders is to re-render the entire application whenever there is a change. It is not saying that Virtual DOM is faster than direct manipulation of the DOM&lt;/p&gt;

&lt;p&gt;No matter how the data changes, it will try to update the DOM with the least cost. When the DOM tree is huge, traversing the previous and current trees is still quite expensive, especially when it is just a small modification at the top level by &lt;code&gt;setState()&lt;/code&gt; leads to traversing the entire tree by default. (We can improve this by using memo hooks)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.1 What is React key?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keys are identifiers used by React to track which elements in the list have been modified, added, or removed. During the development process, we need to ensure that the key of an element is unique among its sibling elements.&lt;/p&gt;

&lt;p&gt;In the React Diff algorithm, React will use the Key value of the element to determine whether the element is newly created or moved, thereby reducing unnecessary element re-rendering&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;key must be unique to each element in the array&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;do not use the index&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;do not use an unstable key such as random number&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;8. Stateless Component&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A stateless component has no state (:)), it means that you can’t reach &lt;code&gt;this.state&lt;/code&gt; inside it. It also has no lifecycle so you shouldn't use those methods and hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;9. How to get the DOM element in React 16.8?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;const refContainer = useRef(initialValue);&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;useRef&lt;/code&gt; returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function TextInputWithFocusButton() {
  const inputEl = useRef(null);
  const onButtonClick = () =&amp;gt; {
    // `current` points to the mounted text input element
    inputEl.current.focus();
  };
  return (
    &amp;lt;&amp;gt;
      &amp;lt;input ref={inputEl} type="text" /&amp;gt;
      &amp;lt;button onClick={onButtonClick}&amp;gt;Focus the input&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can't visit the refs during render phase as the DOM has not been stablished.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81seqdla55r4bsrnhoip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81seqdla55r4bsrnhoip.png" alt="React Life Cycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;10. React Portals&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;ReactDOM.createPortal(child, container)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Elements need to be insert into a different location in the DOM. A typical use case for portals is when a parent component has an overflow: hidden or z-index style, but you need the child to visually “break out” of its container. For example, dialogs, hovercards, and tooltips.&lt;/p&gt;

&lt;p&gt;An event fired from inside a portal will propagate to ancestors in the containing React tree, even if those elements are not ancestors in the DOM tree. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;11. How to avoid unnecessary re-renders in React 16.8 ?&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;React.memo is a new API comes from React 16.6. It is used to cache component rendering and avoid unnecessary updates. It is a high-level component that's very similar to PureComponent. The difference is that React.memo can only be used for functional components.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;12. The mechanism behind React context&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We can use the Javascript clousure as an analogy, the Context object provided by the React component is actually like a clousure provided to child components to access. The properties of the Context object can be regarded as active objects in the scope. &lt;/p&gt;

&lt;p&gt;Since the Context of a component is composed of all the components on the parent node chain through &lt;code&gt;getChildContext（）&lt;/code&gt;, the Context object returned by the component can access the Context properties from all its parent component.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;13. Uncontrolled Components&lt;/strong&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In most cases, we recommend using controlled components to implement forms. In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself.&lt;/p&gt;

&lt;p&gt;Since an uncontrolled component keeps the source of truth in the DOM, it is sometimes easier to integrate React and non-React code when using uncontrolled components. It can also be slightly less code if you want to be quick and dirty. Otherwise, you should usually use controlled components.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>HTML - The one and only guide you need (in progress)</title>
      <dc:creator>WEI FENG</dc:creator>
      <pubDate>Wed, 01 Dec 2021 05:14:21 +0000</pubDate>
      <link>https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga</link>
      <guid>https://dev.to/weifengnusceg/my-frontend-developer-learning-route-html-3mga</guid>
      <description>&lt;p&gt;&lt;em&gt;Sharing the concept I've picked up for HTML. Updates regularly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. How does Doctype work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DOCTYPE also known as the html document type declaration. The doctype declaration would tell the browser which rendering mode to use for that document to be analysed by the browser.&lt;/p&gt;

&lt;p&gt;Syntax example for HTML5: &lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Full standards mode renders pages according to the W3C web standards.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Quirks mode renders pages in a non standards compliant way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Almost standards mode is close to full standards mode, but features support for a small number of quirks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. What's the difference between src and href?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;src&lt;/code&gt; attribute is used to embed a resource, usually URLs into a document for &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags. The loading and processing of the page is paused until this the browser fetches, compiles and executes the file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;href&lt;/code&gt; (Hypertext Reference) attribute specifies the location of a Web resource. The browser understands that this resource is a stylesheet and the processing parsing of the page is not paused (rendering might be paused since the browser needs the style rules to paint and render the page).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Async vs Defer in script tag?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.sertmedia.com/defer-vs-async-attributes-their-impact-on-performance/"&gt;click here to read more&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both async and defer load scripts asynchronously without blocking the DOM, but there are two differences between async and defer.&lt;/p&gt;

&lt;p&gt;Defer waits for the DOM to be loaded. Async doesn’t.&lt;br&gt;
The difference is async doesn’t care whether the DOM is fully loaded. defer waits for DOM to be loaded before it executes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dkMgGFRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gc5ovlsiqu3i6z58d8h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dkMgGFRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gc5ovlsiqu3i6z58d8h.png" alt="Image description" width="880" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;p&gt;You should use defer if your script requires the DOM.&lt;br&gt;
If you use async and you need the DOM, you run the risk that the element you need cannot be found (which is a potential source of bugs).&lt;/p&gt;

&lt;p&gt;You should use async if your script contains the following conditions:&lt;/p&gt;

&lt;p&gt;The DOM you need is already present (or the script doesn’t need the DOM)&lt;br&gt;
The script doesn’t depend on other scripts&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Frequently used meta tags&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The meta tag is defined by the name and content attributes, used to describe the attributes of the web page document, such as the author of the web page, web page description, keywords, etc.&lt;/p&gt;

&lt;p&gt;Commonly used meta tags:&lt;br&gt;
(1) charset, used to describe the encoding type of HTML documents:&lt;br&gt;
&lt;code&gt;&amp;lt;meta charset="UTF-8"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(2) keywords, page keywords:&lt;br&gt;
&lt;code&gt;&amp;lt;meta name="keywords" content="keywords" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(3) description, page description:&lt;br&gt;
&lt;code&gt;&amp;lt;meta name="description" content="page description content" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(4) Refresh, page redirection and refresh:&lt;br&gt;
&lt;code&gt;&amp;lt;meta http-equiv="refresh" content="0;url=" /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(5) viewport, adapted to the mobile terminal, can control the size and ratio of the viewport:&lt;br&gt;
&lt;code&gt;&amp;lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Among them, the content parameter has the following types:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;width viewport:&lt;/strong&gt; width (device-width)&lt;br&gt;
&lt;strong&gt;height viewport:&lt;/strong&gt; height (device-height)&lt;br&gt;
&lt;strong&gt;initial-scale:&lt;/strong&gt; initial scale&lt;br&gt;
&lt;strong&gt;maximum-scale:&lt;/strong&gt; maximum zoom ratio&lt;br&gt;
&lt;strong&gt;minimum-scale:&lt;/strong&gt; minimum zoom ratio&lt;br&gt;
&lt;strong&gt;user-scalable:&lt;/strong&gt; Whether to allow the user to zoom (yes/no)&lt;/p&gt;

&lt;p&gt;(6) Search engine indexing method:&lt;br&gt;
&lt;br&gt;
The content parameter has the following types:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;all:&lt;/strong&gt; The file will be retrieved, and the link on the page can be queried;&lt;br&gt;
&lt;strong&gt;none:&lt;/strong&gt; The file will not be retrieved, and the link on the page cannot be queried;&lt;br&gt;
&lt;strong&gt;index:&lt;/strong&gt; the file will be retrieved;&lt;br&gt;
&lt;strong&gt;follow:&lt;/strong&gt; The link on the page can be queried;&lt;br&gt;
&lt;strong&gt;noindex:&lt;/strong&gt; The file will not be retrieved;&lt;br&gt;
&lt;strong&gt;nofollow:&lt;/strong&gt; The links on the page cannot be queried.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Why we use HTML Semantic Elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/semantic-html5-elements/"&gt;click here to read more&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Semantic HTML elements are those that clearly describe their meaning in a human- and machine-readable way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, it is much easier to read. This is probably the first thing you will notice when looking at the first block of code using semantic elements. &lt;/p&gt;

&lt;p&gt;It has greater accessibility. Search engines and assistive technologies (like screen readers for users with a sight impairment) are also able to better understand the context and content of your website, meaning a better experience for your users.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Common semantic elements: *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.&amp;lt;header&amp;gt;&amp;lt;/header&amp;gt;

2.&amp;lt;nav&amp;gt;&amp;lt;/nav&amp;gt; 

3.&amp;lt;section&amp;gt;&amp;lt;/section&amp;gt;  

4.&amp;lt;main&amp;gt;&amp;lt;/main&amp;gt;  

5.&amp;lt;article&amp;gt;&amp;lt;/article&amp;gt; 

6.&amp;lt;aside&amp;gt;&amp;lt;/aside&amp;gt; 

7.&amp;lt;footer&amp;gt;&amp;lt;/footer&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Pros and Cons for Iframe&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; tag specifies an inline frame.&lt;/p&gt;

&lt;p&gt;An inline frame is used to embed another document within the current HTML document.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It can load resources with huge volume such advertisements as iframe can load the scripts concurrently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It can achieve cross-subdomain communication&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;iframe will block the main page's onload event&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if third party websites were embedded, there may be safety concerns such as CSRF attack.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. What does the label do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The  element is used to associate a text label with a form  field. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. What is New in HTML5?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Semantic elements such as header, nav and footer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;media element: &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;audio : &lt;code&gt;&amp;lt;audio src='' controls autoplay loop='true'&amp;gt;&amp;lt;/audio&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;video : &lt;code&gt;&amp;lt;video src='' poster='imgs/aa.jpg' controls&amp;gt;&amp;lt;/video&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;source: &lt;code&gt;&amp;lt;video&amp;gt; &amp;lt;source src='aa.flv' type='video/flv'&amp;gt;&amp;lt;/source&amp;gt;&lt;br&gt;
&amp;lt;source src='aa.mp4' type='video/mp4'&amp;gt;&amp;lt;/source&amp;gt; &amp;lt;/video&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
