<?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: J. P. Knight</title>
    <description>The latest articles on DEV Community by J. P. Knight (@actionphp).</description>
    <link>https://dev.to/actionphp</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%2F103379%2F74aceefc-6798-42d9-bb55-8f4d29dc2ec7.jpeg</url>
      <title>DEV Community: J. P. Knight</title>
      <link>https://dev.to/actionphp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/actionphp"/>
    <language>en</language>
    <item>
      <title>If you're struggling with JavaScript `this`...</title>
      <dc:creator>J. P. Knight</dc:creator>
      <pubDate>Thu, 15 Nov 2018 00:49:35 +0000</pubDate>
      <link>https://dev.to/actionphp/if-youre-struggling-with-javascript-this-2m06</link>
      <guid>https://dev.to/actionphp/if-youre-struggling-with-javascript-this-2m06</guid>
      <description>&lt;p&gt;Before you can understand what &lt;code&gt;this&lt;/code&gt; is, and why it matters, you first have to understand what an &lt;em&gt;object&lt;/em&gt; is. Why? Because &lt;code&gt;this&lt;/code&gt; &lt;strong&gt;&lt;em&gt;almost&lt;/em&gt;&lt;/strong&gt; always points to an object. The trick lies in knowing how to figure out which object it is pointing to. But, we will come to that in a little bit.&lt;/p&gt;

&lt;p&gt;For now, imagine that you're able to shrink yourself using JavaScript magic. You're now in a JavaScript world. You're a tiny being, looking around. Every &lt;strong&gt;thing&lt;/strong&gt; you see is an object. Some objects are naturally part of the JavaScript world. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;functions&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;window&lt;/code&gt; object ( we will go deeper into that later as well)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, you have other objects made by developers, such as yourself. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Person&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Product&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Account&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To start with, let's look at the objects that human beings create. We create objects because they allow us to work with data in a way that is similar to the world we live in. Objects represent the things we see and use in real life.&lt;/p&gt;

&lt;p&gt;So, in real life, you may have an object called "Book". A book has a title, an author, and a cover. All these things are known as &lt;em&gt;properties&lt;/em&gt; of the book. &lt;/p&gt;

&lt;p&gt;We will begin with the easiest way to create an object. Later, when things get weird, we will look at some more complicated stuff.&lt;/p&gt;

&lt;p&gt;So, the easiest way to create an object is to assign a variable to curly braces like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Person = {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, we have created an empty object called &lt;code&gt;Person&lt;/code&gt;. It has no traits or characteristics. Or, we could say, on a simple level, we haven't assigned any &lt;em&gt;properties&lt;/em&gt; to the object.&lt;/p&gt;

&lt;p&gt;Let's do that - we will give the object some properties. How about giving the &lt;code&gt;Person&lt;/code&gt; a name and age. Here is one way to do it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.name = "J. P. Knight";
Person.age = "127";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, in JavaScript, you can add a property to any object. To do that, place a dot after the object's name and then add the actual name of the property. Assign it a value using the &lt;code&gt;=&lt;/code&gt; operator and you've just created a property!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person.hobby = "running in circles";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, you can also add properties when creating the object. Here is how that code would look.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Person = {
    name: "J. P. Knight",
    age: "127"
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To quickly recap: you can create ( or &lt;em&gt;define&lt;/em&gt; ) the properties as you create your object. Or, you can add properties after you've already created the object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Exercise 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create an object called &lt;code&gt;Account&lt;/code&gt;&lt;br&gt;
When you create it, define at least three properties such as number, name, and status. Assign whatever values you see fit.&lt;br&gt;
After you're done creating your object, type&lt;br&gt;
    console.log(Account);&lt;br&gt;
to see your object and its properties.&lt;/p&gt;

&lt;p&gt;Then, add another property using the dot notation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus points:&lt;/strong&gt; Create an object and call it whatever you like. Give it some properties. Then, use &lt;code&gt;console.log&lt;/code&gt; to view it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you do the above exercise, you're ready for the next step...=&amp;gt;&lt;a href="http://jslearner.com/javascript-this-first-touch.html" rel="noopener noreferrer"&gt;http://jslearner.com/javascript-this-first-touch.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript `this`: 3 Reasons Why You Find It Hard to Grasp</title>
      <dc:creator>J. P. Knight</dc:creator>
      <pubDate>Wed, 14 Nov 2018 20:26:58 +0000</pubDate>
      <link>https://dev.to/actionphp/javascript-this-3-reasons-why-you-find-it-hard-to-grasp-3lpe</link>
      <guid>https://dev.to/actionphp/javascript-this-3-reasons-why-you-find-it-hard-to-grasp-3lpe</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d_3vVqPs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/io6voc0lc4pbvcbtzryl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d_3vVqPs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/io6voc0lc4pbvcbtzryl.jpg" alt="What is this?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are struggling with JavaScript &lt;code&gt;this&lt;/code&gt;, it's NOT YOUR FAULT! The truth is, it can be a tricky subject. It trips up a lot of people, not just you.&lt;/p&gt;

&lt;p&gt;If you're learning JavaScript as your first language, fear not. I've tried to assume as little as possible. You can also get in touch if there is something you believe should be changed or explained in more detail.&lt;/p&gt;

&lt;p&gt;If you're coming from another language, you should know that JavaScript &lt;code&gt;this&lt;/code&gt; has its own way of acting. It is simply not the same as what you're used to. Don't worry, soon you'll have a full grasp of how &lt;code&gt;this&lt;/code&gt; works.&lt;/p&gt;

&lt;p&gt;It's crucial that you have the right mind frame when learning JavaScript &lt;code&gt;this&lt;/code&gt;. For this reason, I decided to start with the reasons why you might struggle with &lt;code&gt;this&lt;/code&gt;. Here we go...&lt;/p&gt;

&lt;h2&gt;
  
  
  1 - You must first grasp a couple of other JavaScript concepts
&lt;/h2&gt;

&lt;p&gt;You have to know certain JavaScript concepts before you can grasp &lt;code&gt;this&lt;/code&gt;. Especially when it gets weird - 'coz it does get weird. That's why you'll first learn the basics of JavaScript objects.&lt;/p&gt;

&lt;p&gt;As you progress, you'll discover a lot of cool stuff that will help you have a deep grasp of &lt;code&gt;this&lt;/code&gt;. You will then become a better, more confident developer. You will have skills that will serve you way beyond using &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2 - You can't know what &lt;code&gt;this&lt;/code&gt; is just by looking at the code where it's created
&lt;/h2&gt;

&lt;p&gt;This is a big one. When you come across &lt;code&gt;this&lt;/code&gt; in code, you can only figure out part of what &lt;code&gt;this&lt;/code&gt; is supposed to be. Why? Because where &lt;code&gt;this&lt;/code&gt; is created has almost no bearing on what it represents when the code is run. So, don't beat yourself up!&lt;/p&gt;

&lt;h2&gt;
  
  
  3 - You are too hard on yourself
&lt;/h2&gt;

&lt;p&gt;Look, being a self-taught developer is no easy task. You should pat yourself on the back for having the courage to even attempt such a feat. With nothing but your sheer guts, you're learning one of the most valuable skills of the modern age. Think about it - if it were easy, everyone would be doing it.&lt;/p&gt;

&lt;p&gt;Some JavaScript concepts are tricky. After all, the language was created in just a week! Yet, we have to leave with all the design decisions, good and bad. The point is, be patient with yourself.&lt;/p&gt;

&lt;p&gt;It's like riding a bicycle - you'll fall and get bruised a few times. But, once you've got it, you have it for the rest of your life. You will be a proud master of a skill that will have a direct impact on your career.&lt;/p&gt;

&lt;p&gt;So, take your time. Take a break after each section. Reward yourself for each bit of progress you make, no matter how tiny it may seem. With that in mind, let's start grasping &lt;code&gt;this&lt;/code&gt; by looking at &lt;a href="http://jslearner.com/javascript-this-objects.html"&gt;JavaScript objects&lt;/a&gt;... &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>thisrocks</category>
      <category>beginners</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
