<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kohei Arai on Medium]]></title>
        <description><![CDATA[Stories by Kohei Arai on Medium]]></description>
        <link>https://medium.com/@koheiarai94?source=rss-2d42db58014d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Oxz3Zf5x6FraIW2t.jpg</url>
            <title>Stories by Kohei Arai on Medium</title>
            <link>https://medium.com/@koheiarai94?source=rss-2d42db58014d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:24:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@koheiarai94/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I have written a diary for 2000 days]]></title>
            <link>https://medium.com/@koheiarai94/i-have-written-a-diary-for-2000-days-fa6bee4b7fcd?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa6bee4b7fcd</guid>
            <category><![CDATA[internships]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sat, 11 May 2019 10:03:50 GMT</pubDate>
            <atom:updated>2019-05-11T10:11:11.047Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sxkpNrdNxKGW0JLTZY6lJg.jpeg" /><figcaption>I visited the Golden Gate Bridge today as there is a scene in The Internship</figcaption></figure><p>I have written a diary for 2000 days. I remember when it reached 1000 days. I achieved the goals I set for living in the United States. However, I was not close to the goals I set when I started writing a diary and had no clue how to accomplish them.</p><p>Then, 1000 days have passed. These recent 1000 days were the period that my long-time goal came true. On 2013/12/27 when is the 39th day since I started writing a diary, I wrote:</p><blockquote>I am back in the United States. I don’t feel home yet.</blockquote><blockquote>But, I <strong>will</strong> make here my home.</blockquote><p>And this was the time I watched the movie <a href="https://www.imdb.com/title/tt2234155/">The Internship</a> on the airplane.</p><p>This is the comedy movie two salesmen join the internship at Google with a bunch of young, tech-savvy geniuses. It’s funny and interesting. What made this movie interesting to me was the culture of Google. I got interested in working at Google and set it my goal, “the internship at Google.”</p><p>Unfortunately, I was not able to achieve that goal. I bombed on the first phone interview for the 2015 summer internship.</p><p>A long time passed. They extended me full-time software engineer offer recently. And I will start working on 2019/05/13.</p><p>It took me 1961 days to achieve my goal.</p><p>Is it a long time? Or, is it a short time? It depends on a person how they see it. If one wants to be an NBL player, it would take more than 10 years. From my perspective, I don’t feel it’s long nor short. It just required me that much time to achieve my goal. (Of course thanks to my parents and friends)</p><p>However, this is not the end of the goal. I want to be a better software engineer, and there are things I would like to be able to do. So, I will keep working hard for the next 1000 days too. I am excited about how my life will be after the next 1000 days.</p><p>2019.05.10 Kohei Arai</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa6bee4b7fcd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[60 LeetCode questions to prepare for coding interview]]></title>
            <link>https://medium.com/@koheiarai94/60-leetcode-questions-to-prepare-for-coding-interview-8abbb6af589e?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8abbb6af589e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[job-hunting]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Tue, 30 Apr 2019 20:30:00 GMT</pubDate>
            <atom:updated>2019-04-30T20:51:28.992Z</atom:updated>
            <content:encoded><![CDATA[<h3>60 LeetCode problems to solve for coding interview</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Qf76NP2wXuaauriSugJciQ.png" /></figure><p>I recently received a job offer from one of FAANG. Here are some problems to help me pass the coding interview. Categories are</p><ul><li>LinkedList</li><li>Stack</li><li>Heap, PriorityQueue</li><li>HashMap</li><li>Graph, BFS, DFS</li><li>Tree, BT, BST</li><li>Sort</li><li>Dynamic Programming</li><li>Binary search</li><li>Recursion</li><li>Sliding window</li><li>Greedy + Backtracking</li></ul><p>If you can solve them quickly, you would have a high chance to pass coding interview. Problems are either Easy or Medium. I put these questions in <a href="https://docs.google.com/spreadsheets/d/1Y98QKaYPazWImEt1nA_ocpGNJ-yQjH1FAsVQhUQ7OTw/edit?usp=sharing">Google Spreadsheet</a>. Feel free to copy and paste to keep track of the progress.</p><p>These problems are also available on <a href="https://leetcode.com/list/xo2bgr0r/">LeetCode List</a>. You can clone to your account to avoid solving problems you have already solved.</p><h3>LinkedList</h3><ul><li><a href="https://leetcode.com/problems/linked-list-cycle/">Linked List Cycle</a></li><li><a href="https://leetcode.com/problems/linked-list-cycle-ii/">Linked List Cycle II</a></li><li><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-list/">Remove Duplicates from Sorted List</a></li><li><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/">Remove Duplicates from Sorted List II</a></li><li><a href="https://leetcode.com/problems/add-two-numbers/">Add Two Numbers</a></li></ul><h3>Stack</h3><ul><li><a href="https://leetcode.com/problems/valid-parentheses/">Valid Parentheses</a></li><li><a href="https://leetcode.com/problems/reverse-linked-list/">Reverse Linked List</a></li></ul><h3>Heap, PriorityQueue</h3><ul><li><a href="https://leetcode.com/problems/kth-largest-element-in-a-stream/">Kth Largest Element in a Stream</a></li><li><a href="https://leetcode.com/problems/top-k-frequent-elements/">Top K Frequent Elements</a></li><li><a href="https://leetcode.com/problems/find-k-pairs-with-smallest-sums/">Find K Pairs with Smallest Sums</a></li></ul><h3>HashMap</h3><ul><li><a href="https://leetcode.com/problems/two-sum/">Two Sum</a></li><li><a href="https://leetcode.com/problems/group-anagrams/">Group Anagrams</a></li><li><a href="https://leetcode.com/problems/intersection-of-two-arrays/">Intersection of Two Arrays</a></li><li><a href="https://leetcode.com/problems/unique-email-addresses/">Unique Email Addresses</a></li><li><a href="https://leetcode.com/problems/first-unique-character-in-a-string/">First Unique Character in a String</a></li><li><a href="https://leetcode.com/problems/subarray-sum-equals-k/">Subarray Sum Equals K</a></li></ul><h3>Graph, BFS, DFS</h3><ul><li><a href="https://leetcode.com/problems/number-of-islands/">Number of Islands</a></li><li><a href="https://leetcode.com/problems/max-area-of-island/">Max Area of Island</a></li><li><a href="https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/">Number of Connected Components in an Undirected Graph</a></li><li><a href="https://leetcode.com/problems/word-ladder/">Word Ladder</a></li></ul><h3>Tree, BT, BST</h3><ul><li><a href="https://leetcode.com/problems/maximum-depth-of-binary-tree/">Maximum Depth of Binary Tree</a></li><li><a href="https://leetcode.com/problems/minimum-depth-of-binary-tree/">Minimum Depth of Binary Tree</a></li><li><a href="https://leetcode.com/problems/merge-two-binary-trees/">Merge Two Binary Trees</a></li><li><a href="https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/">Convert Sorted Array to Binary Search Tree</a></li><li><a href="https://leetcode.com/problems/path-sum/">Path Sum</a></li><li><a href="https://leetcode.com/problems/binary-tree-level-order-traversal/">Binary Tree Level Order Traversal</a></li><li><a href="https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/">Binary Tree Zigzag Level Order Traversal</a></li><li><a href="https://leetcode.com/problems/validate-binary-search-tree/">Validate Binary Search Tree</a></li><li><a href="https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">Construct Binary Tree from Preorder and Inorder Traversal</a></li></ul><h3>Sort</h3><p>Check out <a href="https://www.toptal.com/developers/sorting-algorithms">Sorting Algorithms Animations</a>. Understand in which data set radix sort or insertion sort are better than general heap/merge sort. Go each of sorting algorithms and understand pros and cons.</p><h3>Dynamic Programming</h3><ul><li><a href="https://leetcode.com/problems/paint-fence/">Paint Fence</a></li><li><a href="https://leetcode.com/problems/longest-increasing-subsequence/">Longest Increasing Subsequence</a></li><li><a href="https://leetcode.com/problems/maximum-subarray/">Maximum Subarray</a></li><li><a href="https://leetcode.com/problems/unique-paths/">Unique Paths</a></li><li><a href="https://leetcode.com/problems/unique-paths-ii/">Unique Paths II</a></li><li><a href="https://leetcode.com/problems/house-robber/">House Robber</a></li><li><a href="https://leetcode.com/problems/house-robber-ii/">House Robber II</a></li><li><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/">Best Time to Buy and Sell Stock</a></li><li><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/">Best Time to Buy and Sell Stock II</a></li><li><a href="https://leetcode.com/problems/word-break/">Word Break</a></li><li><a href="https://leetcode.com/problems/coin-change/">Coin Change</a></li></ul><h3>Binary Search</h3><ul><li><a href="https://leetcode.com/problems/search-insert-position/">Search Insert Position</a></li><li><a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/">Find Minimum in Rotated Sorted Array</a></li><li><a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">Search in Rotated Sorted Array</a></li><li><a href="https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/">Capacity To Ship Packages Within D Days</a></li></ul><h3>Recursion</h3><ul><li><a href="https://leetcode.com/problems/powx-n/">Pow(x, n)</a></li><li><a href="https://leetcode.com/problems/k-th-symbol-in-grammar/">K-th Symbol in Grammar</a></li><li><a href="https://leetcode.com/problems/split-bst/">Split BST</a></li></ul><h3>Sliding Window</h3><ul><li><a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">Longest Substring Without Repeating Characters</a></li><li><a href="https://leetcode.com/problems/minimum-size-subarray-sum/">Minimum Size Subarray Sum</a></li></ul><h3>Greedy + Backtracking</h3><ul><li><a href="https://leetcode.com/problems/permutations/">Permutations</a></li><li><a href="https://leetcode.com/problems/subsets/">Subsets</a></li><li><a href="https://leetcode.com/problems/combination-sum/">Combination Sum</a></li><li><a href="https://leetcode.com/problems/generate-parentheses/">Generate Parentheses</a></li></ul><h3>Others</h3><p>I could not put these problems in above categories, but good to solve.</p><ul><li><a href="https://leetcode.com/problems/move-zeroes/">Move Zeroes</a></li><li><a href="https://leetcode.com/problems/meeting-rooms/">Meeting Rooms</a></li><li><a href="https://leetcode.com/problems/meeting-rooms-ii/">Meeting Rooms II</a></li><li><a href="https://leetcode.com/problems/is-subsequence/">Is Subsequence</a></li><li><a href="https://leetcode.com/problems/next-permutation/">Next Permutation</a></li><li><a href="https://leetcode.com/problems/string-to-integer-atoi/">String to Integer (atoi)</a></li><li><a href="https://leetcode.com/problems/zigzag-conversion/">ZigZag Conversion</a></li></ul><h3>Summary</h3><p>If you complete all questions and looking for more problem sets, I recommend checking out <a href="https://github.com/yangshun/tech-interview-handbook/tree/master/algorithms">Algorithm Questions</a>. They also summarize LeetCode problems by category.</p><p>If you finish Algorithm Questions too, check out <a href="https://github.com/liyin2015/Algorithms-and-Coding-Interviews/blob/master/Preparing_for_the_real-world_software_engineering.pdf">Algorithms and Coding Interviews</a>. My friend is writing it, and it’s worth to read. It provides more detail explanation and general approaches to the problems.</p><p>I mentioned these links in the middle of the stories, but here are links to keep track of your progress. Feel free to use them!</p><ul><li><a href="https://docs.google.com/spreadsheets/d/1Y98QKaYPazWImEt1nA_ocpGNJ-yQjH1FAsVQhUQ7OTw/edit?usp=sharing">Google Spreadsheet</a></li><li><a href="https://leetcode.com/list/xo2bgr0r/">LeetCode problem list</a></li></ul><p>Good luck on your job preparation and coding interviews!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8abbb6af589e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Gatsby] How to lazyload images in markdown]]></title>
            <link>https://medium.com/@koheiarai94/gatsby-how-to-lazyload-images-in-markdown-8e4a964aa950?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e4a964aa950</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[gatsbyjs]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Wed, 17 Apr 2019 15:26:01 GMT</pubDate>
            <atom:updated>2019-04-17T15:26:01.773Z</atom:updated>
            <content:encoded><![CDATA[<p>This story is perfect if</p><ul><li>You are using <a href="https://www.gatsbyjs.org/packages/gatsby-remark-images/?=gatsby-remark-images">gatsby-remark-images</a> plugin</li><li>You want to lazyload images used in markdown</li></ul><h3>Install gatsby-remark-lazy-load</h3><p>Install <a href="https://www.gatsbyjs.org/packages/gatsby-remark-lazy-load/?=lazy-load">gatsby-remark-lazy-load</a>:</p><p>npm install --save gatsby-remark-lazy-load</p><p>This plugin does two things:</p><ol><li>Changes src to data-src and srcset to data-srcset of imgtag generated by gatsby-remark-images plugin</li><li>Add lazyload class to img tag.</li></ol><p>By these changes, <a href="https://github.com/aFarkas/lazysizes">lazysizes</a> can lazyload pictures.</p><h3>Add lazysizes to gatsby-browser.js</h3><p>Install <a href="https://github.com/aFarkas/lazysizes">lazysizes</a>.</p><p>npm install --save lazysizes</p><p>Modify gatsby-browser.jsto have them.</p><pre>// gatsby-browser.js<br>import &#39;lazysizes&#39;</pre><p>And check your blog! If everything is working fine, this should enable lazyloading of images!</p><h3>Example</h3><p>My Japanese blog implements lazyload.</p><p>The blog post: <a href="https://1kohei1.com/google/">https://1kohei1.com/google/</a></p><p>Repo: <a href="https://github.com/1kohei1/1kohei1">https://github.com/1kohei1/1kohei1</a></p><p>Let me know your suggestions/comments in the comment below!</p><h3>Resource</h3><p>I took inspiration from <a href="https://github.com/gatsbyjs/gatsby/issues/2288">[gatsby-images] [gatsby-remark-images] Customizing html of a markdown to implement lazy-loading</a> especially from <a href="https://github.com/gatsbyjs/gatsby/issues/2288#issuecomment-407836852">this comment</a> and <a href="https://github.com/gatsbyjs/gatsby/issues/2288#issuecomment-444384934">this comment</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e4a964aa950" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Notes from “You Don’t Know JS: Types & Grammar]]></title>
            <link>https://medium.com/@koheiarai94/notes-from-you-dont-know-js-types-grammar-805140c947ee?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/805140c947ee</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sat, 12 Jan 2019 17:29:27 GMT</pubDate>
            <atom:updated>2019-01-12T17:29:27.921Z</atom:updated>
            <content:encoded><![CDATA[<p>Here are notes I took while studying famous notebook to understand Javascript; <a href="https://github.com/getify/You-Dont-Know-JS/tree/master/types%20%26%20grammar">You Don’t Know JS: Types &amp; Grammar</a>. Each note is summarized per each chapter.</p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch1.md">Chapter 1: Types</a></p><p>Built-in Types are</p><ul><li>null</li><li>undefined</li><li>boolean</li><li>number</li><li>string</li><li>object</li><li>symbol — added in ES6</li></ul><p>Everything other than object is called primitives.</p><p>If you want to check whether or not the variable is null or not, do !a &amp;&amp; typeof a === ‘object. null is the only value that’s falsy and returns “object” for typeof. The lodash checks if the value is null by a === null. So this also works.</p><p>The typeof function returns function regardless it’s function declaration nor function expression.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/084fc8047556bedcbf6c55d407998570/href">https://medium.com/media/084fc8047556bedcbf6c55d407998570/href</a></iframe><blockquote>Variables don’t have types — values have types.</blockquote><p>When typeof is used, it asks the type of the <strong>value</strong>, not the type of the variable.</p><p>Applying typeof to undeclared variable also returns “undefined” Referencing a variable which may not be declared at the runtime, it’s better to use the typeof so that it doesn’t generate ReferenceError.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7da17a387050596fb68d529aad266354/href">https://medium.com/media/7da17a387050596fb68d529aad266354/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch2.md">Chapter 2: Values</a></p><p>If you skip an element in assigning an array, it will create a slot and put undefined.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ebb61d740132c5c65e6bc2b73dafacc/href">https://medium.com/media/1ebb61d740132c5c65e6bc2b73dafacc/href</a></iframe><p>In JS, strings are immutable. You cannot change the contents in the place. One common way to handle reversing a string is Array.prototype.reverse.call(str), but this doesn’t handle unicode mixed characters. The following code cannot handle the all string reverse either.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e64b36407d02b0ba309824c217291f12/href">https://medium.com/media/e64b36407d02b0ba309824c217291f12/href</a></iframe><p>JavaScript Number uses “IEEE 754” standard. When you directly call the method on the number, you have to be careful.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11444e4d901ad2e5c7e8fd5c191ebcd8/href">https://medium.com/media/11444e4d901ad2e5c7e8fd5c191ebcd8/href</a></iframe><p>null is a special keyword, but undefined actually is an identifier. That means you can declare a variable for undefined.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b643896e90d24384bcdb62ac5fbfe5be/href">https://medium.com/media/b643896e90d24384bcdb62ac5fbfe5be/href</a></iframe><p>void operator returns udnefined no matter what’s passed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7345cd63bab78e8221fcab103c713950/href">https://medium.com/media/7345cd63bab78e8221fcab103c713950/href</a></iframe><p>Primitives other than objects are copy-value. But objects are always the reference-copy. This becomes confusing in such code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/172fe3bfce24f8d8ca53b856759e31bf/href">https://medium.com/media/172fe3bfce24f8d8ca53b856759e31bf/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch3.md">Chapter 3: Natives</a></p><p>There are some native classes around the primitive. Such examples are:</p><ul><li>String</li><li>Number</li><li>Boolean</li><li>Array</li><li>Object</li><li>Function</li><li>RegExp</li><li>Date</li><li>Error</li><li>Symbol</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41896d00bf7e0edeb2f253a9d0ba5660/href">https://medium.com/media/41896d00bf7e0edeb2f253a9d0ba5660/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch4.md">Chapter 4: Coercion</a></p><p>Coercion always results the one of primitive types.</p><p><strong>ToString</strong></p><p>There is built in string conversion for some primitives.</p><ul><li>null =&gt; &quot;undefined&quot;</li><li>undefined =&gt; &quot;undefined&quot;</li><li>true =&gt; &quot;true&quot;</li><li>For regular objects, toString() returns the internal [[Class]] like &quot;[object Object]</li></ul><p>If its own toString is defined, it will call its toString instead of object prototypes.</p><p>Array is overridden to join the elements by ,.</p><p>JSON.stringify() converts undefined, function, and symbol to undefined. If such value is found in the array, it will be replaced with `null`. If found in the object, that key-val will be excluded.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0346e0f24e90fefebf4b3c5ce4b51c2/href">https://medium.com/media/a0346e0f24e90fefebf4b3c5ce4b51c2/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=805140c947ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Notes from “You Don’t Know JS: this & Object prototypes]]></title>
            <link>https://medium.com/@koheiarai94/notes-from-you-dont-know-js-this-object-prototypes-663f2df100bc?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/663f2df100bc</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sat, 12 Jan 2019 17:06:15 GMT</pubDate>
            <atom:updated>2019-01-12T17:06:15.357Z</atom:updated>
            <content:encoded><![CDATA[<p>Here are notes I took while studying famous notebook to understand Javascript; <a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&amp;%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes">You Don’t Know JS: this &amp; Object Prototypes</a>. Each note is summarized per each chapter.</p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch1.md">Chapter 1: this Or That?</a></p><p>I thought this is pointing to the function itself. However, that’s not correct. Think about below code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccd58807ee71b06d8f4723d306c54ba4/href">https://medium.com/media/ccd58807ee71b06d8f4723d306c54ba4/href</a></iframe><p>If this points to the function itself, it should print 5 at the last line. This happens since this is not pointing to the function object at all. What foo and this in the function are pointing totally different thing. This can be clearly verified by printing out what’s this inside the function foo.</p><p>It prints out the Window object!</p><p>Another example to investigate what this is actually referring to is this code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee0a4afd6f71a28f4165098be9444e22/href">https://medium.com/media/ee0a4afd6f71a28f4165098be9444e22/href</a></iframe><p>When a is called directly, this refers to the Window object. When a is executed inside the variable x, it refers to the x object. So I guess this is an execution context?</p><p>If you want to refer this inside the function, there are a couple of ways.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b05dce97492ae6a0de60c3eb8def3ec1/href">https://medium.com/media/b05dce97492ae6a0de60c3eb8def3ec1/href</a></iframe><p>this is not an author-time binding but a runtime binding.</p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch2.md">Chapter 2: this All Makes Sense Now!</a></p><p>Rules of this binding.</p><p><strong>Default Binding</strong></p><p>If function is invoked standalone, this refers to the global object. And this is the default behavior.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fdd24009e47de96d0aefe7afb5bb245/href">https://medium.com/media/2fdd24009e47de96d0aefe7afb5bb245/href</a></iframe><p>In here, foo is called with a plain, un-decorated function reference. If the code inside the function is running in strict mode, this rule doesn’t apply.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/687d38a2ae267bd39a3733a2ac51ed94/href">https://medium.com/media/687d38a2ae267bd39a3733a2ac51ed94/href</a></iframe><p><strong>Implicit Binding</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cedda730bf2f33ba999dcd703afe49e3/href">https://medium.com/media/cedda730bf2f33ba999dcd703afe49e3/href</a></iframe><p>This is what I use mostly. The object literal contains a reference to the object and the function refers to the called object by this. If function is invoked by reference, the caller is bonded to this.</p><p>Even nested, the object which refers the function is bounded to this object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df81fb38e378f0bf0f822f995cfc81b8/href">https://medium.com/media/df81fb38e378f0bf0f822f995cfc81b8/href</a></iframe><p>Implicit Lost</p><p>When a function is not called by reference (meaning no object before .), it falls back to the default binding. This is sometimes confusing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/692225e1bd392b0d69de74f89518507e/href">https://medium.com/media/692225e1bd392b0d69de74f89518507e/href</a></iframe><p>When the function is passed to another function, it also loses the context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0fecab1165b3b2b445d3962ddbcee27/href">https://medium.com/media/c0fecab1165b3b2b445d3962ddbcee27/href</a></iframe><p>This is because when the function is passed to doFoo, it already lost the reference to obj. It’s a standalone function. When standalone function is called, the default binding applies.</p><p>One note: if you changed var to let, this changes. Since let registers a variable to the lexical scoping instead of the global scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b54f909e646ad7cf093c13f1de546ec8/href">https://medium.com/media/b54f909e646ad7cf093c13f1de546ec8/href</a></iframe><p>This clearly tells let creates a variable at the scope different from where var creates a variable. var is function or global scoped and let is block based. So variables declared by let can only be referred in that scope.</p><p><strong>Explicit Binding</strong></p><p>You can specify what object will be bound to this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0263334cfba853923cd43ec22cfe58f9/href">https://medium.com/media/0263334cfba853923cd43ec22cfe58f9/href</a></iframe><p>There are ways to keep this among function calls. However, I will not summarize as it doesn’t help.</p><p><strong>new Binding</strong></p><p>Like other OOP, JS also has keyword of new. However, this has nothing to do with OOP concept of new.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8944d6dfd8c8aada2e160af756b9a30/href">https://medium.com/media/e8944d6dfd8c8aada2e160af756b9a30/href</a></iframe><p>When new is applied to the function, it creates a new instance of the function. this is automatically bounded to the new instance.</p><p>For basic rules are covered:</p><ul><li>Default bindings</li><li>Implicit bindings</li><li>Explicit bindings</li><li>new Bindings</li></ul><p>What’s the order of these bindings? The order of preference is as follows. The top one applies first and move down to check if they apply.</p><ol><li>new Binding</li><li>Explicit binding</li><li>Implicit binding</li><li>Default binding</li></ol><p>Although there is exception. If explicit binding is applied and the passed value to call, apply, and bind is null or undefined, default binding applies instead of implicit bindings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22cde4b83b831ce717d4db626ce7996a/href">https://medium.com/media/22cde4b83b831ce717d4db626ce7996a/href</a></iframe><p>Lexical this. Arrow function adopts the this binding from the enclosing scope. They are essentially a syntactic replacement of self = this in pre-ES6 coding.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b78c179ced04ffc1e0e8b70972d64e0/href">https://medium.com/media/0b78c179ced04ffc1e0e8b70972d64e0/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch3.md">Chapter 3: Objects</a></p><p>How object is created:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d189f8d2e058f274357c9819ceaefed5/href">https://medium.com/media/d189f8d2e058f274357c9819ceaefed5/href</a></iframe><p>Objects are one of primary types in JS:</p><ol><li>string</li><li>number</li><li>boolean</li><li>null</li><li>undefined</li><li>object</li></ol><p>Although typeof null returns object, it’s not actually the object. The return of typeof null is incorrect.</p><p><strong>The statement “everything in JavaScript is an object” is not true</strong> since there are primitives other than the object like above. However, when the function is called on the primitive, JS engine converts primitive to the corresponding built-in object. This looks like everything is an object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5f0663fa217dd7163499d05b341964f/href">https://medium.com/media/c5f0663fa217dd7163499d05b341964f/href</a></iframe><p>string, number, boolean has an object wrapper. null and undefined do not have object wrapper. Date can only be created by constructive form. Object, Array, Function, RegExp are the objects regardless of whether the literal or constructed form is used.</p><p>The value in the objects are stored in the area called locations. You can access the contents of the object by .a <strong>property access</strong> or [“a”] <strong>key access</strong>. Object keys are always the string. When the key is not string, it’s coerced to the string.</p><p>ES6 added a feature called computed property names. This registers a variable as a key on the object construction.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51762c70e419867d86e3fb80cf91c43b/href">https://medium.com/media/51762c70e419867d86e3fb80cf91c43b/href</a></iframe><p>Arrays are objects, so you can add a property to the array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ec1d76cf0161e380218b3a1deec1e73/href">https://medium.com/media/1ec1d76cf0161e380218b3a1deec1e73/href</a></iframe><p>Duplicating an object is a tricky problem. ES6 Object.assign does a shallow copy. It iterates its owned keys and assign whatever the value by =.</p><p>Since ES5, you can define the object property.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce438f2495d871f60be4d8a14348c40a/href">https://medium.com/media/ce438f2495d871f60be4d8a14348c40a/href</a></iframe><p>enumerable is used for enumeration such as for…in. Setting this false makes the property not displayed in such enumeration. However, it’s still accessible by the property access or key access.</p><p>By setting writable: false and configurable: false, you can make a constant property in the object instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/869126cd84bbd74425ccb03d60f7b7b9/href">https://medium.com/media/869126cd84bbd74425ccb03d60f7b7b9/href</a></iframe><p>Object [[Get]] and [[Put]] does more work than it seems to be doing. You can customize getter and setter since ES5.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d310a3d44be732ac7199c0489b4e29fa/href">https://medium.com/media/d310a3d44be732ac7199c0489b4e29fa/href</a></iframe><p>To check if the property exists in the object, there are two ways. in or Object.hasOwnProperty. The difference is if the check the object’s [[Prototype or not.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b96e77444bf69e94afabc5c6fd1482d6/href">https://medium.com/media/b96e77444bf69e94afabc5c6fd1482d6/href</a></iframe><p>More deep dive into enumerable property configuration. enumerable means “will be included if the object’s properties are iterated through”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17a88b0de178d78eaafed0fcc5050e0b/href">https://medium.com/media/17a88b0de178d78eaafed0fcc5050e0b/href</a></iframe><p>You can also define your iterator on the object like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54de0d5b0ec64c9064f8c56011966cd0/href">https://medium.com/media/54de0d5b0ec64c9064f8c56011966cd0/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch4.md">Chapter 4: Mixing (Up) “Class” Objects</a></p><p>Polymorphism is a concept that inherited class can access its public properties/methods in the parent class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/434e70101e7f10d91d1903f0550e2152/href">https://medium.com/media/434e70101e7f10d91d1903f0550e2152/href</a></iframe><p>Polymorphism is a copying over the parent stuff to the child class. So ignition implementation in the super is overridden with child class implementation.</p><p>JS way to do polymorphism.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/770141078127e45227e8e8415ed50ec8/href">https://medium.com/media/770141078127e45227e8e8415ed50ec8/href</a></iframe><p>The function Vehicle.ignition is updated, but it’s not reflected to Car.ignition. Reference to the function is copied.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*J1ASJBiLU43JvhJ1kWlVdA.png" /></figure><p>mixin is to have methods which belong to the other instances without inheriting the objects/instances.</p><p>Polymorphism is having different functions at multiple levels of an inheritance chain with the same name.</p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch5.md">Chapter 5: Prototypes</a></p><p>If object has a property, it uses its value. If it doesn’t have it, it proceeds [[Prototype]] chain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abb754e1728df01ba0eddca0bbdded9d/href">https://medium.com/media/abb754e1728df01ba0eddca0bbdded9d/href</a></iframe><p>Object.create creates an object whose prototype is passed object. Look up in prototypes continue until it finds a matched property or prototype chain ends. This prototype chain look-up also applies to the enumerable. If you use for...in loop, it travels prototype chain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d501a5191186b5b3541a281e29453c40/href">https://medium.com/media/d501a5191186b5b3541a281e29453c40/href</a></iframe><p>The end of prototype chain is Object.prototype This includes various utility functions like .toString() and .valueOf().</p><p>This Prototype chain causes some trouble when you try to assign a value to the property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*EeBy9sh1tjgpSggLvalC5Q.png" /></figure><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch6.md">Chapter 6: Behavior Delegation</a></p><p>I started taking notes on frontendmasters.com <a href="https://frontendmasters.com/courses/javascript-foundations/">Deep JavaScript Foundations</a> by the author of You Dont Know JS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=663f2df100bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Notes from “You Don’t Know JS: Scope & Closures”]]></title>
            <link>https://medium.com/@koheiarai94/notes-from-you-dont-know-js-scope-closures-530da14ff4c0?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/530da14ff4c0</guid>
            <category><![CDATA[ydkjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sun, 02 Dec 2018 19:54:16 GMT</pubDate>
            <atom:updated>2018-12-02T19:54:16.714Z</atom:updated>
            <content:encoded><![CDATA[<p>Here are notes I took while studying famous notebook to understand Javascript; <a href="https://github.com/getify/You-Dont-Know-JS/tree/master/scope%20%26%20closures">You Don’t Know JS: Scope &amp; Closure</a>. Each note is summarized per each chapter.</p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch1.md">Chapter 1: What is Scope?</a></p><p>JS program is compiled on the fly. This type of language is called “Interpreter” language.</p><p>For example, when JS Engine compiles a statement var a = 2; It does two things.</p><ol><li>Encountering var a, Compiler asks Scope to check if a exists. If exits, move on. Otherwise, Compiler asks Scope to create a new variable a</li><li>Compiler then generates a code to assign a to be 2.</li></ol><p>When the Engine actually runs a code produced by the Compiler, it first checks where a is stored in the Scope, then assign it. When the Engine doesn’t find a, it raises an error.</p><p>When the engine looks up a variable, it’s interested in either what’s stored in the variable or where to assign a value. Depending on the purpose, the Engine executes two types of variable look-up: “LHS” lookup or “RHS” lookup.</p><blockquote>“LHS” look-up is trying to find where to store a value. “RHS” look-up is trying to retrieve the value assigned to the variable.</blockquote><p>When the variable is not found in the current scope, it goes up till the global Scope. The reason why we distinguish “LHS” and “RHS” is because it behaves differently when the Scope doesn’t have a variable.</p><p>When “RHS” could not find a variable, it raises a ReferenceError. By contrast, if “LHS” look-up reaches the global level without finding it, and if the program is not running in “Strict Mode”, the global Scope will create a new variable in the global scope and keeps running. If “Strict Mode” is enabled, this behavior is not allowed and it raises ReferenceError as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/176a80c54448f5a4031eb9c2fb1a2db0/href">https://medium.com/media/176a80c54448f5a4031eb9c2fb1a2db0/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch2.md">Chapter 2: Lexical Scope</a></p><p>Scope: the set of rules that govern how the Engine can look up a variable by its identifier name.</p><ul><li>Lexical scope (used by the vast majority of programming language)</li><li>Dynamic scope (used in bash scripting)</li></ul><p>Global variables are also automatically properties of the global object. =&gt; Is it different from global scope?</p><p>cheating lexical scope leads to poorer performance</p><p>eval receives a string as a parameter and treat that code as it has been there at the author time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dba8fb81c9e10feb1b6b1a763a22e6a6/href">https://medium.com/media/dba8fb81c9e10feb1b6b1a763a22e6a6/href</a></iframe><p>with is used to make multiple objects assignment easy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf6e48ea50f48d5ecbe9ec51b4d0c7b9/href">https://medium.com/media/bf6e48ea50f48d5ecbe9ec51b4d0c7b9/href</a></iframe><p>However, this sometimes leaking a variable to the global scope. This is because with treats a given objects as a separate lexical scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c912e1c013ae4e6c517c92337642fd39/href">https://medium.com/media/c912e1c013ae4e6c517c92337642fd39/href</a></iframe><p>a is defined since o2 doesn’t have it and LHS look-up reaches the global scope and creates a new variable.</p><p><strong>DO NOT USE EVAL AND WITH</strong></p><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch3.md">Chapter 3: Functions vs Block Scope</a></p><p>When does the Scope created?</p><p>When you want to execute a block of codes without making a function and calling it, you do this way</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2d33ed70bb3fc0571b7dc1f17764bb2/href">https://medium.com/media/f2d33ed70bb3fc0571b7dc1f17764bb2/href</a></iframe><p>Instead of treating a function as a standard declaration, the function is treated as a function-expression. The easy way to distinguish declaration vs expression is the position of the word “function.” If “function” is the very first thing in the statement, then it’s a function declaration. Otherwise, it’s a function expression.</p><p>The key difference between the function declaration and function expression is where the function name is bound as an identifier. Function declaration bounds to enclosing scope. Otherwise, function expression bounds to the scope the function creates. So it doesn’t pollute the enclosing scope.</p><p>The variables declared with var always belong to the enclosing scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65ff6b429256628f96f788c9cec23551/href">https://medium.com/media/65ff6b429256628f96f788c9cec23551/href</a></iframe><p>let attaches the variable declaration to the scope of whatever block. let doesn’t hoist to the entire scope of the block they appear in. You cannot use a variable declared with let before the actual declaration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb5a9c5ac0d3085106b7a4b8448fe783/href">https://medium.com/media/cb5a9c5ac0d3085106b7a4b8448fe783/href</a></iframe><p>When the Engine compiles the second program, the compiler adds xyz to the scope. However, it’s not defined till it goes to the second line.</p><p>catch clause is also a block.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8df661462122ba69c337e52ce7657e9b/href">https://medium.com/media/8df661462122ba69c337e52ce7657e9b/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch4.md">Chapter 4: Hoisting</a></p><p>What’s the order of variable declaration and assignment? Copying each snippet from the below and running in chrome developer tool helps you to get some hint about this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c2e942c06657697622b6d5157710033/href">https://medium.com/media/2c2e942c06657697622b6d5157710033/href</a></iframe><p>Since the engine adds variables to the scope before actually running a code, all variable/functions are already in the scope. Only the declaration are hosted, while any assignments or other executable logic are left in place.</p><p>Function declarations are hoisted, but function expressions are not.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/035690f779637b61bff8f4d04de3f1e8/href">https://medium.com/media/035690f779637b61bff8f4d04de3f1e8/href</a></iframe><p>foo is in the scope, but its value is undefined. That’s why it had TypeError. bar is in the function expression, therefore it can be only referred by itself.</p><p>Functions are hosted first, and then variables. However, the later implementation overrides.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ecc0d6787ebd954f70f7f9f55f071f0/href">https://medium.com/media/9ecc0d6787ebd954f70f7f9f55f071f0/href</a></iframe><p><a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch5.md">Chapter 5: Scope Closure</a></p><p>Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f637771df2622759fcce36a04b69c63/href">https://medium.com/media/6f637771df2622759fcce36a04b69c63/href</a></iframe><p>What happens here is even after the function foo is executed, bar keeps the reference to the inner lexical scope of foo and it’s not collected by garbage collector. The reference bar has to foo’s lexical scope is called the <strong>Closure</strong>.</p><p>More practical example of closure is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b875a6980b12ee1fe750358ca1a56f5a/href">https://medium.com/media/b875a6980b12ee1fe750358ca1a56f5a/href</a></iframe><p>Understand closure in deep. Look at below code and expect what happens.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecccf7d830e2719f770979975a8109b3/href">https://medium.com/media/ecccf7d830e2719f770979975a8109b3/href</a></iframe><p>This prints out 5 five times per 1 second after its execution. The reason why this happens is because the function timer uses the global scope. i in the global scope is 5since var creates a new variable in the scope containing a declaration not in the block it’s declared.</p><p>How to solve this? IIFE. When IIFE is created, it creates its own lexical scope.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e034aec29e6d3f65bb71c8ae8dd1aed8/href">https://medium.com/media/e034aec29e6d3f65bb71c8ae8dd1aed8/href</a></iframe><p>This still doesn’t work. Since IIFE lexical scope does not have the variable i and it refers to the global scope by RHS look-up. So this is how it makes to work with using IIFE.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abe373518ea4d846e9ee90ea3cc609d6/href">https://medium.com/media/abe373518ea4d846e9ee90ea3cc609d6/href</a></iframe><p>In the first example, IIFE scope contains j with the value of i when it’s called. This way, when the timer function is executed, it refers to the internal scope first, then prints out.</p><p>Changing var to let also solves this problem. It’s because when let is used inside the for-loop, it creates a new variable per iteration! So each i inside the for loop is treated as a different variable in below code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a692b1b7ed81206e1bb2a890345d6d2/href">https://medium.com/media/8a692b1b7ed81206e1bb2a890345d6d2/href</a></iframe><p>This closure is heavily used when creating a module. Think about the following code example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9e08683d0ef8779b2f28e1f846fd390/href">https://medium.com/media/d9e08683d0ef8779b2f28e1f846fd390/href</a></iframe><p>This is a common pattern you see on the JS code. The reason why you can call doSomething and doAnother in the instance of CoolModule is because doSomething and doAnother has a closure to the lexical scope of CoolModule.</p><h3>Summary</h3><p>This chapter was about scoping and closure. When JS Engine executes JS program, first the compiler adds a variable to the scope. This step is what’s called Hoisting in JS. Therefore, it doesn’t result ReferenceError to use a variable declared in the later code. The function declaration takes more priority than the variable declaration. However, they can be overridden.</p><p>When the Engine looks up the variable in the scope, it operates either “LHS” look-up or “RHS” look-up. “LHS” look-up is used to assign a variable, and “RHS” look-up is used to retrieve a value in the variable. Without Strict mode, “LHS” look-up creates a new variable in the global scope when the variable is not found. By contrast, “RHS” look-up returns ReferenceError.</p><p>The closure is the concept that a function declared in the lexical scope keeps a reference to it’s enclosing scope even executed outside of its original lexical scope. This is used to create a module in JS. However, this has to be careful since when a function accessing lexical scope, it’s lexical scope has changed since the declaration (eg: setTimeout)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=530da14ff4c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Next app locally and deploy to now]]></title>
            <link>https://medium.com/@koheiarai94/build-next-app-locally-and-deploy-to-now-ee1dcef95282?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee1dcef95282</guid>
            <category><![CDATA[now]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sun, 07 Jan 2018 04:45:30 GMT</pubDate>
            <atom:updated>2018-01-07T04:45:30.175Z</atom:updated>
            <content:encoded><![CDATA[<p>If your project fails during build, it’s likely due to shortage in memory. 512MB is the amount of RAM allocated for OSS plan. So we need to build locally and deploy to now.</p><p>The process is:</p><ol><li>Build locally</li><li>Track .next folder in the project (now does not upload untracked folder/file)</li><li>Change package.json build script (so that now does not build in the platform)</li><li>Deploy current folder</li></ol><p>This is the shell script that handles all the steps:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3445fd1c203bb898e2acaf839afee96/href">https://medium.com/media/d3445fd1c203bb898e2acaf839afee96/href</a></iframe><p>You can create a powershell script similar to this if you are using Windows.</p><p>Once you are ready to deploy, just run sh deploy.sh in your terminal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee1dcef95282" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatically run ng build on heroku deploy]]></title>
            <link>https://medium.com/@koheiarai94/automatically-run-ng-build-on-heroku-deploy-2477cf7a8ae0?source=rss-2d42db58014d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2477cf7a8ae0</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mean-stack]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[heroku]]></category>
            <dc:creator><![CDATA[Kohei Arai]]></dc:creator>
            <pubDate>Sun, 07 May 2017 21:40:38 GMT</pubDate>
            <atom:updated>2018-04-17T20:19:32.202Z</atom:updated>
            <content:encoded><![CDATA[<p>This story is for people who followed the youtube playlists <a href="https://www.youtube.com/playlist?list=PLillGF-RfqbZMNtaOXJQiDebNXjVapWPZ">MEAN Stack Front To Back</a> to set up MEAN environment.</p><p>I didn’t want to run ng build every time when I deploy to heroku. So here is how to run it automatically. We will utilize npm postbuild and postinstall.</p><h4>Run npm install for your angular app</h4><p>When your mean app is deployed to heroku, they will use package.json under the root directory. npm installmust be run for your angular app.</p><p>Open package.json under your parent directory and insert following line to scriptstag:</p><pre>&quot;scripts&quot;: {<br>  ...<br>  &quot;heroku-postbuild&quot;: &quot;cd ANGULAR_APP_FOLDER &amp;&amp; npm install&quot;<br>}</pre><p>heroku-postbuild is a tag used only in Heroku environment. It runs specified command after Heroku builds your application. (<a href="https://devcenter.heroku.com/articles/nodejs-support#heroku-specific-build-steps">Explanation on Heroku Devcenter</a>)</p><h4>Run ng build after the package is installed</h4><p>Move to your angular app folder and open package.json (NOTE: this is ANGULAR APP package.json. Different from package.json at previous step). Insert following line to scriptstag:</p><pre>&quot;scripts&quot;: {<br>  ...<br>  &quot;postinstall&quot;: &quot;ng build --aot -prod&quot;<br>}</pre><p>Command in postinstall is executed after npm package is installed. Now, it’s time to build your angular application. You can check ng cli options <a href="https://github.com/angular/angular-cli/wiki/build">here</a>.</p><h4>Modify devDependencies and dependencies</h4><p>Keep your angular app’s package.jsonopen. Insert following packages to dependencies.</p><p>Angular CLI is running on heroku environment in which Angular CLI is not installed by default. So we have to install Angular CLI at production environment.</p><pre>&quot;@angular/cli&quot;: &quot;^1.0.1&quot;,<br>&quot;@angular/compiler-cli&quot;: &quot;^2.3.1&quot;,<br>&quot;typescript&quot;: &quot;^2.0.3&quot;</pre><p>Remove following libraries from devDependencies. Since they are installed at production, we do not need to keep them in dev environment.</p><pre>&quot;@angular/compiler-cli&quot;<br>&quot;typescript&quot;</pre><h4>Specify what node and npm version you want to use</h4><p>Open package.jsonunder your project folder. Insert following lines:</p><pre>&quot;engines&quot;: {<br>  &quot;node&quot;: &quot;7.8.0&quot;,<br>  &quot;npm&quot;: &quot;4.6.1&quot;<br>}</pre><p>Without this, npm complains UNMET DEPENDENCIES when you push to heroku.</p><p>These two links were very helpful to solve the problem. Thanks.</p><ul><li><a href="https://medium.com/@intellix/production-ready-angular-cli-in-heroku-4bcdc8322550">Production ready Angular CLI v1.0.0 in Heroku</a></li><li><a href="https://github.com/angular/angular-cli/issues/5654">The &quot;@angular/compiler-cli&quot; package was not properly installed. · Issue #5654 · angular/angular-cli</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2477cf7a8ae0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>