<?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: Tay</title>
    <description>The latest articles on DEV Community by Tay (@tcase360).</description>
    <link>https://dev.to/tcase360</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%2F28901%2Ff7259c19-6cfd-4ed7-b138-4c092d0d2b38.jpeg</url>
      <title>DEV Community: Tay</title>
      <link>https://dev.to/tcase360</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tcase360"/>
    <language>en</language>
    <item>
      <title>Have you ever had multiple offers to weigh before?</title>
      <dc:creator>Tay</dc:creator>
      <pubDate>Sat, 12 Sep 2020 18:50:24 +0000</pubDate>
      <link>https://dev.to/tcase360/have-you-ever-had-multiple-offers-to-weigh-before-52lo</link>
      <guid>https://dev.to/tcase360/have-you-ever-had-multiple-offers-to-weigh-before-52lo</guid>
      <description>&lt;p&gt;I see this all the time - "interview around and make sure that you're getting the best deal at a company you value" is the type of saying I see in various areas by bloggers and engineering influencers. I'm curious to see how many engineers actually do get multiple offers to weigh because I feel that while this is good advice, it comes from an assumption a certain amount of leverage that the engineer must have in order to garner multiple offers whether that be through references, experience, expertise, saved money, etc.&lt;/p&gt;

&lt;p&gt;I personally have never had multiple offers and I believe it is because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I have never really had the monetary leverage to wait and interview long enough to get them. &lt;/li&gt;
&lt;li&gt;I am normally really bad at technical interviews.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the question is, &lt;strong&gt;have you ever had multiple offers to weigh before and why&lt;/strong&gt;? &lt;/p&gt;

&lt;p&gt;If you have, what do you think contributed to that (timing, money, experience, etc)? If you haven't and you're comfortable with saying why you took the first offer you got, please reply!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Debounce Deep Dive — JavaScript ES6</title>
      <dc:creator>Tay</dc:creator>
      <pubDate>Tue, 26 Mar 2019 17:04:21 +0000</pubDate>
      <link>https://dev.to/tcase360/debounce-deep-dive--javascript-es6-2g70</link>
      <guid>https://dev.to/tcase360/debounce-deep-dive--javascript-es6-2g70</guid>
      <description>&lt;p&gt;Lately I’ve been asked a lot about how I would implement a debounce function as an exercise, and I wondered why this question has become prevalent in the front-end engineering world.&lt;/p&gt;

&lt;p&gt;The more I was asked this question, the more I thought about why it was asked, and the reasoning I came up with makes sense:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It tests your deeper knowledge of JavaScript&lt;/li&gt;
&lt;li&gt;There is a practical, real-world application&lt;/li&gt;
&lt;li&gt;These are commonly used in modern front-end development&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;When looking around, there wasn’t a wealth of information on the inner workings of a debounce function and that was surprising — I know that Underscore has implementations for both debounce and throttle, but I do believe that it’s important to understand on a deeper level what they are doing before using them extensively. This blog’s purpose is to explain the nuances of JavaScript inside this (albeit, simple) implementation. There are a lot of opinions on the “correct” way to implement these functions, and this blog post is not about that. So without further ado, let’s dive right in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Purpose of Debounce
&lt;/h2&gt;

&lt;p&gt;This function is built in order to limit the amount of times a function is called — scroll events, mousemove events, and keypress events are all great examples of events that we might want to capture, but can be quite taxing if we capture them every single time they fire. In order to combat this, we implement debounce and throttle functions. We won’t discuss the throttle function in this post, but a debounce function will wait until the last time the function is called and then fire after a predetermined amount of time or once the event firing becomes inactive.&lt;/p&gt;




&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at a debounce function implementation in ES6.&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;debounce&lt;/span&gt; &lt;span class="o"&gt;=&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;time&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;timeout&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="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;functionCall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&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;apply&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;arguments&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;timeout&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;timeout&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="nx"&gt;functionCall&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&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;Let's take a look at this step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a wrapper function with two arguments: a callback and an integer for the timeout — this will hold the state of the timeout. Note that the wrapper function will only be called once, when the wrapper function is referenced.&lt;/li&gt;
&lt;li&gt;Declare the &lt;code&gt;timeout&lt;/code&gt; variable, which will be &lt;code&gt;undefined&lt;/code&gt; until the timeout is set in the returned function.&lt;/li&gt;
&lt;li&gt;Return a function — this will be called every time the function is called. Make sure that the function returned is not an arrow function, as you will lose context.&lt;/li&gt;
&lt;li&gt;Apply &lt;code&gt;this&lt;/code&gt; context to callback function, and attach arguments.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;clearTimeout&lt;/code&gt; if timeout exists.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; and pass the applied function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This way, the &lt;code&gt;clearTimeout&lt;/code&gt; resets the timeout each time the function is called, and if the function is not called within the time provided, then it will finally fire the function.&lt;/p&gt;

&lt;p&gt;Using the function would look like this:&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;keyup&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&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;e&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;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first argument being passed is the event handler, and the second is the amount of time in milliseconds that we would consider an element “inactive” after the last event is fired.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;There are a couple parts of this function that can be used as learning points when it comes to JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The returned function will take the arguments that the event handler should get — even if they aren’t explicitly declared in the function declaration. Just use the arguments variable that is automatically created when inside a function.&lt;/li&gt;
&lt;li&gt;fn.apply is very handy, and is perfect for this situation as we won’t always know how many arguments are being provided, therefore we can send the full object through. This will also persist the context of our function.&lt;/li&gt;
&lt;li&gt;The functionCall variable must be declared inside the returned function so we can call it with the correct arguments.&lt;/li&gt;
&lt;li&gt;We must declare the timeout variable, because if we don't pass a variable into clearTimeout, then it will globally clear timeouts, and we wouldn’t want to interfere in the global scope so as to avoid unwanted side-effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is a problem with a simple-looking solution spanning 11 lines, but it covers a lot of different concepts that can show a deeper understanding of JavaScript if done correctly, like persisting &lt;code&gt;this&lt;/code&gt;, returning a function, and the &lt;code&gt;.apply()&lt;/code&gt; method, all encapsulated inside a practical problem that can be used in the real world.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
