<?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>Forem: Robert Tate</title>
    <description>The latest articles on Forem by Robert Tate (@roberttate).</description>
    <link>https://forem.com/roberttate</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%2F880604%2F7cc7f17a-eec2-44d2-802c-57a0c132d004.jpeg</url>
      <title>Forem: Robert Tate</title>
      <link>https://forem.com/roberttate</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/roberttate"/>
    <language>en</language>
    <item>
      <title>AI Wrangling For Devs 101</title>
      <dc:creator>Robert Tate</dc:creator>
      <pubDate>Tue, 29 Aug 2023 16:23:55 +0000</pubDate>
      <link>https://forem.com/devsatasurion/ai-wrangling-for-devs-101-514c</link>
      <guid>https://forem.com/devsatasurion/ai-wrangling-for-devs-101-514c</guid>
      <description>&lt;h2&gt;
  
  
  Tips for Working Alongside Generative AI Tools
&lt;/h2&gt;

&lt;p&gt;The recent wave of generative AI products in their many forms (Large Language Models, Image Generators, etc.) have sparked a sense of wonder, excitement, and perhaps even fear in the hearts of many. &lt;/p&gt;

&lt;p&gt;As a software developer, I've got a lot of existential questions right now. Will I be out of a job in a few years? &lt;/p&gt;

&lt;p&gt;And once that happens, will the robot overlords still look kindly upon me?&lt;/p&gt;

&lt;p&gt;Hopefully I can point to this article to prove that I made every best effort to educate and prepare the humans for their smooth arrival.&lt;/p&gt;

&lt;p&gt;Speaking of which, I was recently tasked with using AI at work and I want to share my learnings. My employer (&lt;a href="https://www.asurion.com/" rel="noopener noreferrer"&gt;Asurion&lt;/a&gt; - great place to work 😁) recently decided to host an AI themed developer hackathon, that we are calling the "Asurion Code Jam".&lt;/p&gt;

&lt;p&gt;The aim of this "jam" will be to provide dedicated work hours for us to try out writing software utilizing new AI tools. Participants will get to learn while building, and we are emphasizing that they don't have to concern themselves with building viable products if they would rather not.&lt;/p&gt;

&lt;p&gt;A hackathon all about education and creativity? I'm in! This was a refreshing approach to me, so I decided to volunteer to help plan the thing.&lt;/p&gt;

&lt;p&gt;Fast forward a few months later and the code jam is starting to get real. Time to create the teams! In the spirit of the theme, &lt;strong&gt;I became tasked to find a way to use AI to group our participants.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following is an accounting of that journey - using ChatGPT Plus.&lt;/p&gt;

&lt;h3&gt;
  
  
  To Hallucinate, or Not to Hallucinate?
&lt;/h3&gt;

&lt;p&gt;Thinking about how to approach this, I was pretty confident that giving ChatGPT (even using the GPT-4 model) over one hundred participant entries with multiple properties for each person and asking it to return them in groups would result in unreliable data. &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%2Fx16101mpz1fr098vkjg8.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%2Fx16101mpz1fr098vkjg8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many examples in the wilds of the internet that demonstrate erratic responses if large language models are fed too much input (nonsensical or otherwise), so I knew I had to find an option that would allow me to mitigate hallucinations altogether.&lt;/p&gt;

&lt;p&gt;We don't want to publish team rosters that includes pretend people, right? That would be awkward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enter Code Interpreter
&lt;/h3&gt;

&lt;p&gt;The new ChatGPT Plus code interpreter tool was the solution that made me realize that I could have my cake and eat it too. AI can do the heavy lifting of writing and testing the algorithm to use for team creation, and I gain the benefit of knowing exactly how the teams were produced. At the end of the day, ChatGPT just hands over some code that I can run myself.&lt;/p&gt;

&lt;p&gt;I still trust imperative code more than I do magic chat bots (for now), so this felt like a great way to leverage these new tools without compromising data integrity.&lt;/p&gt;

&lt;h3&gt;
  
  
  My Process
&lt;/h3&gt;

&lt;p&gt;The following was my process for getting teams created:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I downloaded the spreadsheet with all of the participants from the signup form we sent out to everyone, and resaved it as a &lt;code&gt;csv&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I wrote a simple node.js script to read the &lt;code&gt;csv&lt;/code&gt; file, clean it up a bit, and save it as a &lt;code&gt;json&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter ChatGPT Code Interpreter. &lt;strong&gt;If you'd like to see our full conversation, check it out &lt;a href="https://chat.openai.com/share/f971edd8-262a-4fd5-a86b-e9e9a173ae8f" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt;. At every step of the way, I would take the code snippet of ChatGPT's latest attempt, copy it over into VS Code, run it locally, and assess the results of the outputted &lt;code&gt;csv&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We finally got to a workable solution, but there was a lot of trial and error in getting to the point where the result was exactly what I needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT: I did not give ChatGPT any real participant data.&lt;/strong&gt; If you ever use public facing generative AI sites for work purposes, you need to think critically about what you're providing it. Personally identifiable information (PII) or other sensitive or proprietary data should obviously be a no no.&lt;/p&gt;

&lt;h3&gt;
  
  
  AI Wrangling Key Takeaways
&lt;/h3&gt;

&lt;p&gt;Here is what I learned, which I pass on to you as key takeaways for wrangling your own AI's:&lt;/p&gt;

&lt;h4&gt;
  
  
  Be Crazy Specific
&lt;/h4&gt;

&lt;p&gt;Before the thread with ChatGPT that I linked above, there were other much less successful chat threads where I didn't even get close to a workable solution. &lt;/p&gt;

&lt;p&gt;The difference? Whenever I provided more details and context, I got closer. When I didn't provide that clarity, ChatGPT filled in the gaps by making whatever assumptions it wanted, without even running them by me! How rude.&lt;/p&gt;

&lt;p&gt;One thing I finally tried really helped: &lt;strong&gt;adding a requirements list&lt;/strong&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%2Fjkrlbgxtgdhd56xt7a5l.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%2Fjkrlbgxtgdhd56xt7a5l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So devs: if you are looking to use ChatGPT to help write the code, you need to put on your product owner hats and clearly articulate the desired outcomes (not saying I did that perfectly, but it helped nonetheless).&lt;/p&gt;

&lt;p&gt;If you are having trouble clearly articulating your desired outcomes, figure out why. Is there some missing business context? Is there some ambiguity still floating around in the requirements? Is there some complexity somewhere that needs more unpacking?&lt;/p&gt;

&lt;p&gt;When you start the process strong by providing an extremely clear initial prompt, your refinement process will shorten.&lt;/p&gt;

&lt;p&gt;Speaking of the refinement process...&lt;/p&gt;

&lt;h4&gt;
  
  
  Switch to Feedback When You're Close
&lt;/h4&gt;

&lt;p&gt;Depending on the complexity of the desired code, the ChatGPT code interpreter is probably not going to give you what you need on the first attempt. But if you gave it clear instructions (see my first tip), it might come close. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If it does in fact come close to a workable solution, it's time to start a feedback loop instead of starting over with a better requirements prompt&lt;/strong&gt;. Propose a change, ChatGPT takes another crack at it, and you reassess and repeat as needed.&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%2Fr64lexsbcb3vi9gzxubn.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%2Fr64lexsbcb3vi9gzxubn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This stage of the process feels a bit like pair programming with a junior developer. They made some mistakes but they are super eager to try again and get it right. &lt;/p&gt;

&lt;p&gt;I found through some early trial and error that the process of refining the solution seems to go smoother if you propose one change or fix at a time, and see how ChatGPT responds to it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Don't Turn On Autopilot
&lt;/h4&gt;

&lt;p&gt;My last tip when it comes to AI wrangling is this: don't turn your brain off.&lt;/p&gt;

&lt;p&gt;Initially there were moments where because AI was writing the code, I inadvertently stopped thinking critically about what it was giving me. It gave me a snippet, I tried running it, it didn't work, and so I just started over without incorporating new directions or feedback that were arguably any better.&lt;/p&gt;

&lt;p&gt;A strange sense of laziness was taking over. AI can figure out what I need without me actively participating, right?&lt;/p&gt;

&lt;p&gt;Wrong. Because I was not taking the time to comprehend the code and refine my own understanding, I was not giving valuable feedback and we were not getting anywhere. This lasted until the novelty of my new partnership had worn off, and then I realized I needed to start reading, debugging, and actively participating.&lt;/p&gt;

&lt;p&gt;I think the larger lesson here is that &lt;strong&gt;the output of an AI system is only ever going to be as good as the input it receives&lt;/strong&gt;. So if we as developers are not steering these tools well, we should not expect the tool to make up the difference.&lt;/p&gt;

&lt;p&gt;That makes me a little more hopeful that I'll still have a job in the coming years. 😬&lt;/p&gt;

&lt;h3&gt;
  
  
  Parting Thoughts
&lt;/h3&gt;

&lt;p&gt;As advertised, this is just a 101 course. If you want to get better with asking AI to do your work for you - you'll need to continue experimenting. The developers of tomorrow are going to have to be both wordsmiths and cyberpunks... not just the latter. Hopefully these insights will help as a starting point for the former.&lt;/p&gt;


&lt;div class="ltag__user ltag__user__id__880604"&gt;
    &lt;a href="/roberttate" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&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%2Fuser%2Fprofile_image%2F880604%2F7cc7f17a-eec2-44d2-802c-57a0c132d004.jpeg" alt="roberttate image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/roberttate"&gt;Robert Tate&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/roberttate"&gt;Software Developer doing life in Nashville, Tennessee. I like to write articles about JavaScript, and I try to make them somewhat beginner friendly.&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>chatgpt</category>
      <category>programming</category>
      <category>hackathon</category>
    </item>
    <item>
      <title>React is great...but you should try Lit.</title>
      <dc:creator>Robert Tate</dc:creator>
      <pubDate>Mon, 29 Aug 2022 19:31:58 +0000</pubDate>
      <link>https://forem.com/devsatasurion/react-is-greatbut-you-should-try-lit-3aeb</link>
      <guid>https://forem.com/devsatasurion/react-is-greatbut-you-should-try-lit-3aeb</guid>
      <description>&lt;p&gt;Does anyone else out there need a quick break from React? For just a few minutes maybe?&lt;/p&gt;

&lt;p&gt;I've been using React for years now, and I miss the days where it felt new and exciting. In my brain, React has gone from a sports car to a minivan. It's still &lt;strong&gt;very&lt;/strong&gt; good at what it does, but I'm a little bored of it. I don't get very excited using it anymore. We've lost our spark, you might say.&lt;/p&gt;

&lt;p&gt;So, I thought I would try something new and get &lt;a href="https://lit.dev/" rel="noopener noreferrer"&gt;Lit&lt;/a&gt; for a little while.&lt;/p&gt;

&lt;p&gt;You heard me.&lt;/p&gt;

&lt;p&gt;So what is &lt;a href="https://lit.dev/" rel="noopener noreferrer"&gt;Lit&lt;/a&gt;? Per the "about" section on their &lt;a href="https://github.com/lit/lit/" rel="noopener noreferrer"&gt;github&lt;/a&gt; page: &lt;strong&gt;Lit is a simple library for building fast, lightweight web components.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Their slogan is:&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%2F0sskjud9tnazgy9gk3u5.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%2F0sskjud9tnazgy9gk3u5.png" alt="Simple. Fast. Web Components."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't know what a web component is, take a step backwards here and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components" rel="noopener noreferrer"&gt;read about web components on MDN&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Put simply, web components are a set of native browser APIs that let you create your own HTML elements. Those elements can do whatever you want them to. &lt;strong&gt;Lit is a library that wraps around web components, making them easier to work with.&lt;/strong&gt; Instead of setting up web components using the native APIs directly, you can use Lit to make &lt;strong&gt;Lit Components&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Your next question might be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;That's not what React does at all...so is Lit really comparable?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can I use Lit to build web apps, websites, and user interfaces, just like I can with React? Is that even what Lit is meant for?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yes, &lt;strong&gt;Lit is comparable to React&lt;/strong&gt; in many ways. Yes, &lt;strong&gt;you can use it to build websites, web apps, and UI's&lt;/strong&gt;. And &lt;strong&gt;yes, it's intended to be used for those things&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;It even does some things React can't do (gasp).&lt;/p&gt;

&lt;p&gt;Let's start looking at the differences between a React component and a Lit component. I made a silly little component that takes in a string from user input, and spits it out reversed. Imports not included. And just assume &lt;code&gt;reverseString&lt;/code&gt; does what it says it does, in both cases.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;React Component&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;TextReverser&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;revText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setRevText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&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;inputElem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&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;handleClick&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setRevText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputElem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&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="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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;input&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputElem&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/input&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;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&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;Reverse&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;revText&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&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;revText&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;/h2&amp;gt;&lt;/span&gt;&lt;span class="err"&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="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;Here's the same thing as a &lt;strong&gt;Lit Component&lt;/strong&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;customElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text-reverser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TextReverser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;LitElement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;state&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;revText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&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="nd"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#input&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;inputElem&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;handleClick&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;revText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reverseString&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;inputElem&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&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;html&lt;/span&gt;&lt;span class="s2"&gt;`
     &amp;lt;div&amp;gt;
      &amp;lt;input id="input" type="text"&amp;gt;&amp;lt;/input&amp;gt;
      &amp;lt;button @click=&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;gt;Reverse Me!&amp;lt;/button&amp;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;revText&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="s2"&gt;`&amp;lt;h2&amp;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;revText&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/h2&amp;gt;`&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;
     &amp;lt;/div&amp;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;Oh boy! Such wow! So many new things to talk about and look at.&lt;/p&gt;

&lt;p&gt;First, Lit uses &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;. It's not required, but it's encouraged and it's awesome. It saves you time on some syntax by enabling the use of decorators. It also gives you strong typing. It also gives you code completion. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why would you not want those things?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Second, Lit components are class based. Less wow, if class syntax is not your thing. But if I had to guess, I would think that Lit will eventually get support for function based Lit components. I mean, you gotta stay with the times.&lt;/p&gt;

&lt;p&gt;To compare what's going on...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In React:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I used &lt;code&gt;useState&lt;/code&gt; to manage state for the value of &lt;code&gt;revText&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I used &lt;code&gt;useRef&lt;/code&gt; to grab a reference to the input element DOM node.&lt;/li&gt;
&lt;li&gt;I wrote a &lt;code&gt;handleClick&lt;/code&gt; function which gets called when the button is clicked. It reverses the string and sets the new value for &lt;code&gt;revText&lt;/code&gt; using &lt;code&gt;setRevText&lt;/code&gt;, which causes the component to re-render.&lt;/li&gt;
&lt;li&gt;I rendered html by returning JSX. If &lt;code&gt;revText&lt;/code&gt; has a value, it displays it in an &lt;code&gt;h2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In Lit:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I used the &lt;code&gt;@state&lt;/code&gt; decorator to initialize some internal state for the component: &lt;code&gt;revText&lt;/code&gt;. I access that in other places in the component using &lt;code&gt;this.revText&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I used the &lt;code&gt;@query&lt;/code&gt; decorator to grab a reference to the input element DOM node.&lt;/li&gt;
&lt;li&gt;I wrote a &lt;code&gt;handleClick&lt;/code&gt; method which gets called when the button is clicked. It reverses the string, and reassigns &lt;code&gt;this.revText&lt;/code&gt; to that new value. Because &lt;code&gt;this.revText&lt;/code&gt; was initialized using the &lt;code&gt;@state&lt;/code&gt; decorator, when its value is reassigned, the component re-renders.&lt;/li&gt;
&lt;li&gt;I rendered html in the &lt;code&gt;render&lt;/code&gt; method of the component, by returning a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates" rel="noopener noreferrer"&gt;tagged template literal&lt;/a&gt;, by using Lit's &lt;code&gt;html&lt;/code&gt; tag function. If &lt;code&gt;this.revText&lt;/code&gt; has a value, it displays it in an &lt;code&gt;h2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what did we just learn?&lt;/p&gt;
&lt;h3&gt;
  
  
  They both encapsulate state &amp;amp; behavior.
&lt;/h3&gt;

&lt;p&gt;Lit and React components both generally implement the same concept: encapsulating state and behavior into little interactive UI bits.&lt;/p&gt;

&lt;p&gt;Mmmm....little bits.&lt;/p&gt;
&lt;h3&gt;
  
  
  They are both composable
&lt;/h3&gt;

&lt;p&gt;Lit and React are both component-based. For each library, you can create more complex UI's by composing your components together. I didn't show how to do this in my code examples above, so here's some reading if you want to dig in further:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://lit.dev/docs/composition/overview/" rel="noopener noreferrer"&gt;Composing Lit Components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/components-and-props.html#composing-components" rel="noopener noreferrer"&gt;Composing React Components&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  They are both reactive.
&lt;/h3&gt;

&lt;p&gt;There is a &lt;a href="https://lit.dev/docs/components/lifecycle/" rel="noopener noreferrer"&gt;Lit component lifecycle&lt;/a&gt; (no virtual DOM required) and there is a &lt;a href="https://reactjs.org/docs/react-component.html#the-component-lifecycle" rel="noopener noreferrer"&gt;React component lifecycle&lt;/a&gt;. When the component state changes, the UI reflects that change. &lt;/p&gt;

&lt;p&gt;SIDE NOTE: &lt;strong&gt;they both can use redux&lt;/strong&gt;. &lt;a href="https://vaadin.com/blog/using-redux-in-a-litelement-app" rel="noopener noreferrer"&gt;Check out this article&lt;/a&gt; to learn how to set that up for Lit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ukCFEU6Cg5MCCDoaVN/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ukCFEU6Cg5MCCDoaVN/giphy-downsized-large.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lit is starting to feel pretty similar to React, after unpacking things a little.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what's truly different about Lit?&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Lit is closer to the browser
&lt;/h3&gt;

&lt;p&gt;With Lit, a lot of the heavy lifting is done by native browser APIs, instead of by additional framework boilerplate code that also has to be downloaded.&lt;/p&gt;

&lt;p&gt;I really like this, because it makes sense in my head to use things that are already freely available. You wouldn't make your own ice using ice trays if your fridge already had an ice machine, right?&lt;/p&gt;

&lt;p&gt;Because of the emphasis towards leaning on what the browser already offers us (web components), Lit is a tiny library - about 5kb when minified and compressed. So if you're worried about page load and interested in browser features, Lit makes a good argument for itself.&lt;/p&gt;
&lt;h3&gt;
  
  
  Lit is interoperable (can be used in other things)
&lt;/h3&gt;

&lt;p&gt;Because a Lit component is just a web component at the end of the day, &lt;strong&gt;you can use Lit anywhere!&lt;/strong&gt; You can use Lit on its own, or you can slide Lit components into projects that are using any other front-end framework.&lt;/p&gt;

&lt;p&gt;This is especially exciting for the purposes of implementing a &lt;strong&gt;design system&lt;/strong&gt;, for instance. In one of my previous jobs I helped work on one specific implementation of our companies design system - the one that was used in our CMS. Folks from other teams that used React had to create their own React based design system implementation. And so on and so forth for all the other frameworks we used, of which there were a good number. So you can imagine there was a lot of developer time spent on rebuilding the same components over and over for different frameworks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What if you could just implement your design system once?&lt;/strong&gt; And reuse it everywhere? That's something Lit can offer. If you need to use one of your design system components in React - first build it with Lit. Then, pull that Lit component into React and get going.&lt;/p&gt;

&lt;p&gt;For further reading: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.tderflinger.com/en/litelement-react-app" rel="noopener noreferrer"&gt;Check out this article&lt;/a&gt; on how to integrate a Lit component into a React component.&lt;/li&gt;
&lt;li&gt;Lit also has a separate package you can use called &lt;a href="https://www.npmjs.com/package/@lit-labs/react" rel="noopener noreferrer"&gt;@lit-labs/react&lt;/a&gt;, which provides a utility to make a React component wrapper for a Lit component.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Parting thoughts
&lt;/h3&gt;

&lt;p&gt;The main thing I want you to get from this article: &lt;strong&gt;Lit is cool, and you should give it a try&lt;/strong&gt;! Please hear me when I say this: React is also excellent at what it does, and this article shouldn't be construed as a recommendation to never use React ever again.&lt;/p&gt;

&lt;p&gt;Sometimes it's just nice to branch out, is all.&lt;/p&gt;

&lt;p&gt;As a developer, learning should be a constant priority. So if you're feeling how I was feeling about React, and want to feel that spark again, I recommend learning Lit! They've got incredible &lt;a href="https://lit.dev/docs/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and &lt;a href="https://lit.dev/tutorials/" rel="noopener noreferrer"&gt;interactive tutorials&lt;/a&gt;, by the way.&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you want to read any of my other articles, you can find them at &lt;a href="https://www.quickwinswithcode.com/" rel="noopener noreferrer"&gt;quickwinswithcode.com&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag__user ltag__user__id__880604"&gt;
    &lt;a href="/roberttate" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&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%2Fuser%2Fprofile_image%2F880604%2F7cc7f17a-eec2-44d2-802c-57a0c132d004.jpeg" alt="roberttate image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/roberttate"&gt;Robert Tate&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/roberttate"&gt;Software Developer doing life in Nashville, Tennessee. I like to write articles about JavaScript, and I try to make them somewhat beginner friendly.&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>design</category>
      <category>react</category>
      <category>lit</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Essential "this" concepts in JavaScript</title>
      <dc:creator>Robert Tate</dc:creator>
      <pubDate>Mon, 25 Jul 2022 16:47:00 +0000</pubDate>
      <link>https://forem.com/devsatasurion/essential-this-concepts-in-javascript-2g9h</link>
      <guid>https://forem.com/devsatasurion/essential-this-concepts-in-javascript-2g9h</guid>
      <description>&lt;h2&gt;
  
  
  Execution context, implicit and explicit binding, arrow functions, and of course, this.
&lt;/h2&gt;

&lt;p&gt;To me, a competent  understanding of the keyword &lt;code&gt;this&lt;/code&gt; in JavaScript feels just about as hard to master as the final boss in a D&amp;amp;D campaign.&lt;/p&gt;

&lt;p&gt;I am not prepared.&lt;/p&gt;

&lt;p&gt;My instinct therefore tells me I should write an article on the subject so that I can attempt to move &lt;code&gt;this&lt;/code&gt; from my &lt;strong&gt;passive&lt;/strong&gt; vocabulary to my &lt;strong&gt;active&lt;/strong&gt; vocabulary.&lt;/p&gt;

&lt;p&gt;Join me on this quest, and let's level up together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Levels 1–4: Local Heroes
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Defining execution context, &lt;code&gt;this&lt;/code&gt;, and &lt;code&gt;new&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before we look at &lt;code&gt;this&lt;/code&gt;, it will be helpful to define &lt;strong&gt;execution context&lt;/strong&gt;. MDN describes &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide/In_depth#javascript_execution_contexts"&gt;execution context&lt;/a&gt; in this way:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;When a fragment of JavaScript code runs, it runs inside an execution context. There are three types of code that create a new execution context:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The global context is the execution context created to run the main body of your code; that is, any code that exists outside of a JavaScript function.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each function is run within its own execution context. This is frequently referred to as a "local context."&lt;br&gt;
Using the ill-advised &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval"&gt;&lt;code&gt;eval()&lt;/code&gt;&lt;/a&gt; function also creates a new execution context.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each context is, in essence, a level of scope within your code. As one of these code segments begins execution, a new context is constructed in which to run it; that context is then destroyed when the code exits.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Put simply, &lt;strong&gt;an execution context is a created space that directly surrounds a block of running code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;global&lt;/strong&gt; context is created at the start of a program. Each time a function is invoked, a &lt;strong&gt;local&lt;/strong&gt; execution context gets created and is added to the execution context stack (the call stack).&lt;/p&gt;

&lt;p&gt;I see other articles refer to execution context as the "environment" surrounding your block of running code, which makes sense, because it's more than just a location!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An execution context also has custom properties.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These properties are a little different between global and local context, and between different types of local context (functions vs arrow functions vs classes, etc).&lt;/p&gt;

&lt;p&gt;Something that &lt;strong&gt;every&lt;/strong&gt; execution context has however, is a property known as &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So what is &lt;code&gt;this&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drum roll…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;this&lt;/code&gt; is a reference to an object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But what object? Well, that depends. Time to run through some examples to start figuring it out.&lt;/p&gt;

&lt;p&gt;In our first simple example, we log the value of &lt;code&gt;this&lt;/code&gt; in the global execution context, outside of any function:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="c1"&gt;// window&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the browser, we see that &lt;code&gt;this&lt;/code&gt; equals the &lt;code&gt;window&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;MDN confirms the behavior in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#global_context"&gt;this definition&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In the global execution context (outside of any function), &lt;code&gt;this&lt;/code&gt; refers to the global object whether in strict mode or not.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;SIDE NOTE:&lt;/strong&gt; In the browser the global object is &lt;code&gt;window&lt;/code&gt;. In Node.js, the global object is actually an object called &lt;code&gt;global&lt;/code&gt;. There are some slight behavioral differences though. In Node.js, the example above would actually log the current &lt;code&gt;module.exports&lt;/code&gt; object, not the &lt;code&gt;global&lt;/code&gt; object. 🤷 Let's avoid that rabbit hole for now.&lt;/p&gt;

&lt;p&gt;In this next example, we log &lt;code&gt;this&lt;/code&gt; inside a function:&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="nf"&gt;logThisInAFunction&lt;/span&gt;&lt;span class="p"&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="nf"&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="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;logThisInAFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// window&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, you might think that &lt;code&gt;this&lt;/code&gt; will evaluate to something other than &lt;code&gt;window&lt;/code&gt;, seeing as we created a new local execution context upon invoking the function, which the code inside our function runs within.&lt;/p&gt;

&lt;p&gt;But as a default (when &lt;strong&gt;not&lt;/strong&gt; using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;strict mode&lt;/a&gt;, another rabbit hole 🤷‍), unless &lt;strong&gt;explicitly&lt;/strong&gt; set (which we will get to), &lt;code&gt;this&lt;/code&gt; still references &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;MDN confirms this &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#function_context"&gt;default behavior&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Inside a function, the value of &lt;code&gt;this&lt;/code&gt; depends on how the function is called.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Since the following code is not in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;strict mode&lt;/a&gt;, and because the value of &lt;code&gt;this&lt;/code&gt; is not set by the call, &lt;code&gt;this&lt;/code&gt; will default to the global object, which is &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window"&gt;&lt;code&gt;window&lt;/code&gt;&lt;/a&gt; in a browser.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;SIDE NOTE:&lt;/strong&gt; There is a weird difference in behavior between the browser and Node.js in this second example. In the browser, &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;window&lt;/code&gt;, but in Node.js, &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;global&lt;/code&gt; this time, instead of the &lt;code&gt;module.exports&lt;/code&gt; object. This is due to the difference in how Node.js handles the default value of &lt;code&gt;this&lt;/code&gt; between global and local contexts.&lt;/p&gt;

&lt;p&gt;The next example expands on &lt;code&gt;this&lt;/code&gt; having a different value based on &lt;strong&gt;how&lt;/strong&gt; the function is called:&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="nf"&gt;logThisInAFunction&lt;/span&gt;&lt;span class="p"&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="nf"&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myImportantObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;logThisInMyObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;logThisInAFunction&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;logThisInAFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// window&lt;/span&gt;

&lt;span class="nx"&gt;myImportantObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logThisInMyObject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// myImportantObject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call &lt;code&gt;logThisInAFunction()&lt;/code&gt; and &lt;code&gt;myImportantObject.logThisInMyObject()&lt;/code&gt;, the same function is ultimately being invoked. However, the value of &lt;code&gt;this&lt;/code&gt; is not the same for the two invocations. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#as_an_object_method"&gt;MDN again eloquently describes&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;When a function is called as a method of an object, its &lt;code&gt;this&lt;/code&gt; is set to the object the method is called on.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Put simply, if you call a function as a method of an object, that object is going to become the value for &lt;code&gt;this&lt;/code&gt; instead of the global object. In this way, we are &lt;strong&gt;implicitly&lt;/strong&gt; binding the value of &lt;code&gt;this&lt;/code&gt; to an object of our choosing.&lt;/p&gt;

&lt;p&gt;The next example looks at how &lt;code&gt;this&lt;/code&gt; behaves for &lt;strong&gt;classes&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;class&lt;/span&gt; &lt;span class="nc"&gt;MyAwesomeClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&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="nf"&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="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;awesomeClassInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyAwesomeClass&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// MyAwesomeClass {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When creating an instance of a class using the &lt;code&gt;new&lt;/code&gt; keyword, a new empty object is created and set as the value of &lt;code&gt;this&lt;/code&gt;. Any properties added to &lt;code&gt;this&lt;/code&gt; in the constructor will be properties on that object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#class_context"&gt;As MDN puts it&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The behavior of &lt;code&gt;this&lt;/code&gt; in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;classes&lt;/a&gt; and functions is similar, since classes are functions under the hood. But there are some differences and caveats.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Within a class constructor, &lt;code&gt;this&lt;/code&gt; is a regular object. All non-static methods within the class are added to the prototype of &lt;code&gt;this&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Relatively straight forward.&lt;/p&gt;

&lt;h4&gt;
  
  
  Before we level up…
&lt;/h4&gt;

&lt;p&gt;Hopefully &lt;code&gt;this&lt;/code&gt; is starting to sink in. Having already talked about &lt;strong&gt;implicit&lt;/strong&gt; binding, let us explore the next level: &lt;strong&gt;explicit&lt;/strong&gt; binding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Levels 5–10: Heroes of the Realm
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Defining &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;bind&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Earlier we saw how we can take a function, add it as a method to an object, and when we invoke that method, that object &lt;strong&gt;implicitly&lt;/strong&gt; becomes our new value for &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we want to learn how to &lt;strong&gt;explicitly&lt;/strong&gt; set the value of &lt;code&gt;this&lt;/code&gt; when invoking a function. We can do that with a few different methods: &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt;, and &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, an example using &lt;code&gt;call&lt;/code&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;wizard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fireball&lt;/span&gt;&lt;span class="dl"&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;warlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Warlock&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;eldrich blast&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="nf"&gt;useFavoriteSpell&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="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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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="s2"&gt; the &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="kd"&gt;class&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; used &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;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bobby&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Bobby the undefined used undefined!&lt;/span&gt;

&lt;span class="nx"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bradston&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Bradston the Wizard used fireball!&lt;/span&gt;

&lt;span class="nx"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;warlock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Matt the Warlock used eldrich blast!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first time we invoke &lt;code&gt;useFavoriteSpell&lt;/code&gt;, we have undefined values. &lt;code&gt;this&lt;/code&gt; has defaulted to referencing the &lt;code&gt;window&lt;/code&gt; object, and the properties &lt;code&gt;class&lt;/code&gt; and &lt;code&gt;favoriteSpell&lt;/code&gt; do not exist on window.&lt;/p&gt;

&lt;p&gt;The next two times we invoke &lt;code&gt;useFavoriteSpell&lt;/code&gt;, we are using &lt;code&gt;call&lt;/code&gt; to assign the value of &lt;code&gt;this&lt;/code&gt; to the object of our choosing, and also invoking the function.&lt;/p&gt;

&lt;p&gt;That is what &lt;code&gt;call&lt;/code&gt; does! The first argument of &lt;code&gt;call&lt;/code&gt; is the object you want &lt;code&gt;this&lt;/code&gt; to equal, and the subsequent comma separated arguments are the function arguments.&lt;/p&gt;

&lt;p&gt;You can read more about &lt;code&gt;call&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The next method we will look at is &lt;code&gt;apply&lt;/code&gt;. No additional code example is necessary to understand it, in my opinion. The main difference between &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;call&lt;/code&gt; accepts function arguments one by one in a comma separated list. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;apply&lt;/code&gt; instead accepts all function arguments as one array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Outside of some other small differences, they do the same thing. You can read further about &lt;code&gt;apply&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we have &lt;code&gt;bind&lt;/code&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;wizard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fireball&lt;/span&gt;&lt;span class="dl"&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;warlock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Warlock&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;eldrich blast&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="nf"&gt;useFavoriteSpell&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="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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&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="s2"&gt; the &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="kd"&gt;class&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; used &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;favoriteSpell&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bobby&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Bobby the undefined used undefined!&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useFavoriteWizardSpell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wizard&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;useFavoriteWizardSpell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bradston&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Bradston the Wizard used fireball!&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useFavoriteWarlockSpell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useFavoriteSpell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;warlock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;useFavoriteWarlockSpell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Matt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Matt the Warlock used eldrich blast!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of setting the value of &lt;code&gt;this&lt;/code&gt; and also invoking the function, calling &lt;code&gt;bind&lt;/code&gt; on our function &lt;strong&gt;just returns a new function&lt;/strong&gt;, whose &lt;code&gt;this&lt;/code&gt; value is now equal to the object we passed as the argument of &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can then call our new function whenever we want, and the value of &lt;code&gt;this&lt;/code&gt; has already been set.&lt;/p&gt;

&lt;p&gt;Read more on &lt;code&gt;bind&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Before we level up…
&lt;/h4&gt;

&lt;p&gt;So now we know about execution context, &lt;code&gt;this&lt;/code&gt;, and how to set a value for &lt;code&gt;this&lt;/code&gt; both implicitly, and explicitly with &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;bind&lt;/code&gt;, and &lt;code&gt;apply&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What else is there? To be honest, there is a lot. But let's focus on one thing at a time, and move on to the relationship between &lt;code&gt;this&lt;/code&gt; and arrow functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Levels 11–16: Masters of the Realm
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;How arrow functions affect &lt;code&gt;this&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Earlier, I said that every execution context has a property known as &lt;code&gt;this&lt;/code&gt;. The behavior with arrow functions is a little different, however.&lt;/p&gt;

&lt;p&gt;While the local execution context created by invoking an arrow function &lt;strong&gt;does&lt;/strong&gt; still have a value for &lt;code&gt;this&lt;/code&gt;, it &lt;strong&gt;does not define it for itself&lt;/strong&gt;. It instead will retain the value of &lt;code&gt;this&lt;/code&gt; that was set by the next outer execution context from where the function was invoked.&lt;/p&gt;

&lt;p&gt;For example:&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;logThisInAnArrowFunction&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="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="nf"&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myImportantObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;logThisInMyObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;logThisInAnArrowFunction&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;logThisInAnArrowFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// window&lt;/span&gt;

&lt;span class="nx"&gt;myImportantObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logThisInMyObject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// window&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see when we call &lt;code&gt;myImportantObject.logThisInMyObject()&lt;/code&gt; that even though a new local execution context was created, &lt;code&gt;this&lt;/code&gt; is going to get its value from the next outer execution context. In this case, it is the global execution context. Therefore, &lt;code&gt;this&lt;/code&gt; remains a reference to the &lt;code&gt;window&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Here is another example that hopefully will drive it home:&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;myImportantObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;exampleOne&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="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logThis&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="p"&gt;};&lt;/span&gt;
        &lt;span class="nf"&gt;logThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;exampleTwo&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="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logThis&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="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="nf"&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="p"&gt;};&lt;/span&gt;
        &lt;span class="nf"&gt;logThis&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;myImportantObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exampleOne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// window&lt;/span&gt;

&lt;span class="nx"&gt;myImportantObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exampleTwo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// myImportantObject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example we have two methods we call from &lt;code&gt;myImportantObject&lt;/code&gt;: &lt;code&gt;exampleOne()&lt;/code&gt; and &lt;code&gt;exampleTwo()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we call &lt;code&gt;myImportantObject.exampleOne()&lt;/code&gt;, we are invoking that function as a method of an object, and therefore, &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;myImportantObject&lt;/code&gt; in that local context.&lt;/p&gt;

&lt;p&gt;However, inside that function, we define another function and then execute it.&lt;/p&gt;

&lt;p&gt;As previously mentioned, unless &lt;strong&gt;explicitly&lt;/strong&gt; set, and unless the function is being invoked as a method of an object, &lt;code&gt;this&lt;/code&gt; references the global object, &lt;code&gt;window&lt;/code&gt;. Therefore, we see &lt;code&gt;window&lt;/code&gt; logged.&lt;/p&gt;

&lt;p&gt;When we call &lt;code&gt;myImportantObject.exampleTwo()&lt;/code&gt; however, something different happens. The first part is the same: &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;myImportantObject&lt;/code&gt; inside &lt;code&gt;exampleTwo&lt;/code&gt;. Next, we define an arrow function and then execute it. The difference is, &lt;strong&gt;the arrow function does not define its own value for &lt;code&gt;this&lt;/code&gt;!&lt;/strong&gt; It instead retains the value of &lt;code&gt;this&lt;/code&gt; from the next outer execution context, which in this example, was the local context created by &lt;code&gt;myImportantObject.exampleTwo()&lt;/code&gt;, where this equalled &lt;code&gt;myImportantObject&lt;/code&gt;. So that is what we see logged.&lt;/p&gt;

&lt;h4&gt;
  
  
  Before we level up…
&lt;/h4&gt;

&lt;p&gt;Arrows functions differ from regular functions in plenty of other ways. If you want to read more, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;check out MDN's page on the topic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have made it to this point in the article, it should hopefully mean that things are starting to sink in. Take a moment to relish in your achievement.&lt;/p&gt;

&lt;p&gt;There's only one section left. Let's dive in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Levels 17–20: Masters of the World
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Callbacks.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before I started writing this article, I thought the last section was going to be the most difficult to tackle. In the past, examples of how &lt;code&gt;this&lt;/code&gt; got its value in callback functions confused me.&lt;/p&gt;

&lt;p&gt;With the context I now have, it no longer feels like callback functions add any more complexity to how &lt;code&gt;this&lt;/code&gt; works. We just need to consider what we already know about execution context, and how we invoke a function.&lt;/p&gt;

&lt;p&gt;I guess the final boss is starting to look a little more beatable 😁.&lt;/p&gt;

&lt;p&gt;Here is an example of using &lt;code&gt;this&lt;/code&gt; within callback functions. See if you can guess what the values logged for each will be:&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="nf"&gt;higherOrderFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&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;myImportantObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;callMyCallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;myImportantObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callMyCallback&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="nf"&gt;callbackFunction&lt;/span&gt;&lt;span class="p"&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="nf"&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callbackArrowFunction&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="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="nf"&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="p"&gt;};&lt;/span&gt;


&lt;span class="nf"&gt;higherOrderFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;higherOrderFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackArrowFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Starting with our first function call (where the argument is &lt;code&gt;callbackFunction&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;higherOrderFunction&lt;/code&gt; is invoked, and creates a local execution context. Because it is not implicitly or explicitly set, &lt;code&gt;this&lt;/code&gt; defaults to &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our callback function is invoked inside &lt;code&gt;higherOrderFunction&lt;/code&gt; via &lt;code&gt;myImportantObject.callMyCallback()&lt;/code&gt;. Because our callback is &lt;strong&gt;not&lt;/strong&gt; an arrow function, the created local execution context defines its own &lt;code&gt;this&lt;/code&gt;. And, because that function is being invoked as a method of &lt;code&gt;myImportantObject&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;myImportantObject&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, let's look at the second function call (where our argument is &lt;code&gt;callbackArrowFunction&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;higherOrderFunction&lt;/code&gt; is invoked just as before. Same outcome: &lt;code&gt;this&lt;/code&gt; equals &lt;code&gt;window&lt;/code&gt; in that local context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our callback is once again invoked inside &lt;code&gt;higherOrderFunction&lt;/code&gt;. This time, because our callback &lt;strong&gt;is&lt;/strong&gt; an arrow function, it &lt;strong&gt;does not&lt;/strong&gt; define its own &lt;code&gt;this&lt;/code&gt;. So even though it is being invoked as a method of &lt;code&gt;myImportantObject&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; retains the value of the next outer execution context, which was &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The moral of the story is, callback functions add complexity only in the sense that you have to consider &lt;strong&gt;how&lt;/strong&gt; (and also &lt;strong&gt;where&lt;/strong&gt; for arrow functions) the callback is being invoked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But we were considering that already!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference is that callback functions are passed as arguments to other functions, and so where and how they get invoked is a little different.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parting Thoughts
&lt;/h4&gt;

&lt;p&gt;Hopefully I didn't lose you at some point 😅.&lt;/p&gt;

&lt;p&gt;As a developer, I am always trying to continue my education - and I recognize that there is &lt;a href="https://en.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect"&gt;still so much I do not know&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is a big subject, and I am sure that there are aspects of the concept that I missed, or examples and explanations that could be improved. &lt;strong&gt;Let's help each other by trying to figure out what those were!&lt;/strong&gt; Feel free to leave a comment, and let me know if this article helped you, and/or what could have been improved.&lt;/p&gt;

&lt;p&gt;Thanks for reading! 😄 Check out some of my earlier articles at &lt;a href="https://www.quickwinswithcode.com/"&gt;quickwinswithcode.com&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Resources
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide/In_depth"&gt;In depth: Microtasks and the JavaScript runtime environment&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;&lt;code&gt;this&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;Strict Mode&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call"&gt;&lt;code&gt;Function.prototype.call()&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt;&lt;code&gt;Function.prototype.apply()&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind"&gt;&lt;code&gt;Function.prototype.bind()&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;Arrow function expressions&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
