<?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: Sean Ryan </title>
    <description>The latest articles on DEV Community by Sean Ryan  (@seanpryan).</description>
    <link>https://dev.to/seanpryan</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F153897%2F05058388-f1d0-448e-95d2-c87fd5e19796.jpg</url>
      <title>DEV Community: Sean Ryan </title>
      <link>https://dev.to/seanpryan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seanpryan"/>
    <language>en</language>
    <item>
      <title>A quick and dirty guide to the call, apply, and bind methods</title>
      <dc:creator>Sean Ryan </dc:creator>
      <pubDate>Mon, 20 May 2019 00:41:25 +0000</pubDate>
      <link>https://dev.to/seanpryan/a-quick-and-dirty-guide-to-the-call-apply-and-bind-methods-5fb8</link>
      <guid>https://dev.to/seanpryan/a-quick-and-dirty-guide-to-the-call-apply-and-bind-methods-5fb8</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;High-level summary for the bosses:&lt;/em&gt;&lt;/strong&gt; &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; are methods we can call on functions to make us less subject to the cruel whims of the &lt;code&gt;this&lt;/code&gt; keyword. &lt;br&gt;
&lt;/p&gt;


&lt;p&gt;If you're in the UK and you tell someone to take the lift to the second floor, they'll know you're talking about using the machine that transports people between different floors of a building. In the US, someone hearing the same instruction might initially think someone is about to give them a piggyback ride up a flight of stairs. &lt;/p&gt;

&lt;p&gt;The same is true when writing code — reserved keywords have different meanings depending on the scope, or execution context, in which they're used. In JavaScript, one of the trickiest examples of a keyword that changes its meaning at the drop of a hat is &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is &lt;code&gt;this&lt;/code&gt;?&lt;/strong&gt; &lt;br&gt;
The &lt;code&gt;this&lt;/code&gt; keyword is a pointer that refers to a scope, or execution context, in your program. Which scope it refers to depends on where it is used. When used outside of a declared object, &lt;code&gt;this&lt;/code&gt; points to the global object. (In the browser, the global object is actually the &lt;code&gt;window&lt;/code&gt; object. If you open up the console in the inspector and type &lt;code&gt;console.log(this)&lt;/code&gt;, you will see the &lt;code&gt;window&lt;/code&gt; object and all of its properties and methods logged). &lt;/p&gt;

&lt;p&gt;Used inside of a declared object, &lt;code&gt;this&lt;/code&gt; refers to the closest parent object. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const jim = {
      name: "Jim",
      age: 24,
      printAge: function() {
        return this.age
      }
    }

    const bob = {
      name: "Bob",
      age: 35,
      printAge: function() {
         return this.age
      }
    }

    jim.printAge() // returns 24
    bob.printAge() // returns 35
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Like the rest of us, Jim is getting older. Let's add another method to the &lt;code&gt;jim&lt;/code&gt; object that will take the number of years he has aged, and return his new age:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jim = {
  name: "Jim",
  age: 24,
  printAge: function() {
    return this.age
  },
  increaseAge: function increaseAge(years){
        return this.name + " is now " + (this.age + years) + " years old."
    }
}

const bob = {
  name: "Bob",
  age: 35,
  printAge: function() {
     return this.age
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When we call &lt;code&gt;jim.increaseAge(5)&lt;/code&gt;, it will return &lt;code&gt;Jim is now 29 years old&lt;/code&gt;. But what if we want to use this same method on the &lt;code&gt;bob&lt;/code&gt; object? We could write it out again inside of &lt;code&gt;bob&lt;/code&gt;, but that would be redundant. Instead, we'll use some trickery to redefine &lt;code&gt;this&lt;/code&gt; when we call &lt;code&gt;increaseAge&lt;/code&gt; so that it refers to the &lt;code&gt;bob&lt;/code&gt; object, and the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties therein. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;call&lt;/code&gt; to the rescue&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;call&lt;/code&gt; is a method that we can use to specify what the keyword &lt;code&gt;this&lt;/code&gt; refers to in the function to which we're adding it. The first argument that we pass into &lt;code&gt;call&lt;/code&gt; is known as the &lt;code&gt;thisArg&lt;/code&gt;. This is the object we want &lt;code&gt;this&lt;/code&gt; in the function to refer to. The subsequent arguments are simply the regular arguments that we want to pass into the function. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;call&lt;/code&gt;, we can access the properties and methods of the &lt;code&gt;bob&lt;/code&gt; object when we invoke &lt;code&gt;jim.increaseAge&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jim = {
  name: "Jim",
  age: 24,
  printAge: function() {
    return this.age
  },
  increaseAge: function increaseAge(years){
        return this.name + " is now " + (this.age + years) + " years old."
    }
}

const bob = {
  name: "Bob",
  age: 35,
  printAge: function() {
     return this.age
  }
}

jim.increaseAge.call(bob, 5) // returns "Bob is now 40 years old."
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;apply&lt;/code&gt; allows us to control what &lt;code&gt;this&lt;/code&gt; refers to inside of a function when we call it from outside of the context in which that function was defined. This is the sort of sorcery that allows us to write more versatile and reusable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;apply&lt;/code&gt; is &lt;code&gt;call&lt;/code&gt;'s slightly higher achieving older sibling&lt;/strong&gt;&lt;br&gt;
Like &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;apply&lt;/code&gt; specifies the context that &lt;code&gt;this&lt;/code&gt; in the function will take in its first argument (the &lt;code&gt;thisArg&lt;/code&gt;). &lt;code&gt;call&lt;/code&gt; can only pass a predetermined number of arguments to a function, however, whereas &lt;code&gt;apply&lt;/code&gt; passes an array of arguments to a function that are then unpacked and passed as parameters to the function.&lt;/p&gt;

&lt;p&gt;Let's take a look at how this works: &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj1 = {
    num: 5
}

const obj2 = {
    num: 2
}

const addToObj = function(a, b, c) {
    return this.num + a + b + c
}

let arr1 = [2, 3, 5, 6]
let arr2 = [4, 6, 3, 9]

console.log(addToObj.apply(obj1, arr1)) // logs 15 to the console

console.log(addToObj.apply(obj1, arr2)) // logs 18 to the console

console.log(addToObj.apply(obj2, arr1)) // logs 12 to the console

console.log(addToObj.apply(obj2, arr2)) // logs 15 to the console
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Above, we declare arrays as variables then use these variables as &lt;code&gt;apply&lt;/code&gt;'s second argument to pass them into the function as separate parameters. With &lt;code&gt;call&lt;/code&gt;, we would not be able to do this, as &lt;code&gt;call&lt;/code&gt; requires the functions arguments to be passed as regular comma-separated parameters. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;bind&lt;/code&gt; method: delayed gratification&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;bind&lt;/code&gt; is the third in this family of methods that allow us to redefine the context of &lt;code&gt;this&lt;/code&gt; when we call functions. While the difference between &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; is subtle, the difference between &lt;code&gt;bind&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; is more significant. &lt;/p&gt;

&lt;p&gt;Rather than immediately invoking the function on which it's called, &lt;code&gt;bind&lt;/code&gt; returns a function definition with the keyword &lt;code&gt;this&lt;/code&gt; set to the value of the first argument passed into it (the &lt;code&gt;thisArg&lt;/code&gt;). This is very useful when we know we want to redefine &lt;code&gt;this&lt;/code&gt; in a function, but we don't know which arguments we want to pass into the function. &lt;/p&gt;

&lt;p&gt;Let's go back to &lt;code&gt;bob&lt;/code&gt; and &lt;code&gt;jim&lt;/code&gt; to take a look at how &lt;code&gt;bind&lt;/code&gt; works: &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var bob = {
   firstName: "Bob",
   sayHi: function(){
      return "Hi " + this.firstName
   },
   addNumbers: function(a,b,c,d){
      return this.firstName + " just calculated " + (a+b+c+d)
   }
}

var jim = {
   firstName: "Jim"
}

var jimAdd = bob.addNumbers.bind(jim, 1, 2, 3, 4)

jimAdd() // Jimm just calculated 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, we took the method &lt;code&gt;addNumbers&lt;/code&gt; on the &lt;code&gt;bob&lt;/code&gt; object and called the &lt;code&gt;bind&lt;/code&gt;  method on it to create another function in which &lt;code&gt;this&lt;/code&gt; is redefined as the &lt;code&gt;jim&lt;/code&gt; object. We stored this new function in the variable &lt;code&gt;jimAdd&lt;/code&gt;, then subsequently called &lt;code&gt;jimAdd&lt;/code&gt;. This works, because unlike &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;bind&lt;/code&gt; doesn't immediately invoke the function it's used on. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't know your arguments yet? Get out of your bind with &lt;code&gt;bind&lt;/code&gt;.&lt;/strong&gt; &lt;br&gt;
When we use &lt;code&gt;bind&lt;/code&gt;, we do not always need to know the parameters to the function we are creating — the only argument we need to pass into &lt;code&gt;bind&lt;/code&gt; is the &lt;code&gt;thisArg&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's return once again to Jim and Bob:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var bob = {
   firstName: "Bob",
   sayHi: function(){
      return "Hi " + this.firstName
   },
   addNumbers: function(a,b,c,d){
      return this.firstName + " just calculated " + (a+b+c+d)
   }
}

var jim = {
   firstName: "Jim"
}

var jimAdd2 = bob.addNumbers.bind(jim, 1,2)
jimAdd2(3,7) // returns 'Jim just added 13'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When we create the &lt;code&gt;jimAdd2&lt;/code&gt; function with &lt;code&gt;bind&lt;/code&gt;, we only pass two arguments into the function. When we call it on the following line, we pass two additional arguments, and the function is called with all four arguments. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using &lt;code&gt;bind&lt;/code&gt; with asynchronous code:&lt;/strong&gt; &lt;br&gt;
Another common application of the &lt;code&gt;bind&lt;/code&gt; method is is asynchronous code. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var bob = {
   firstName: "Bob",
   sayHi: function(){
      setTimeout(function(){
        console.log("Hi " + this.firstName)
      },1000)
   }
}

bob.sayHi() // Hi undefined (1000 miliseconds later)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since the closest parent object to the &lt;code&gt;sayHi&lt;/code&gt; method is &lt;code&gt;bob&lt;/code&gt;, it would make sense that &lt;code&gt;sayHi&lt;/code&gt; returns &lt;code&gt;Hi Bob&lt;/code&gt;. However, because &lt;code&gt;setTimeout&lt;/code&gt; is called at a later point in time, the object that it is attached to at the point of execution is not the &lt;code&gt;bob&lt;/code&gt; object, but the &lt;code&gt;window&lt;/code&gt; object.  &lt;/p&gt;

&lt;p&gt;If we still want the context of &lt;code&gt;this&lt;/code&gt; in the &lt;code&gt;setTimeout&lt;/code&gt; function to be the &lt;code&gt;bob&lt;/code&gt; object when it is called, &lt;code&gt;bind&lt;/code&gt; provides a perfect solution. It allows us to explicitly set the context of &lt;code&gt;this&lt;/code&gt; without immediately invoking the function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var bob = {
   firstName: "Bob",
   sayHi: function(){
      setTimeout(function(){
         console.log("Hi " + this.firstName)
     }.bind(this),1000)
   }
}

bob.sayHi() // "Hi Bob"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, we can actually pass &lt;code&gt;this&lt;/code&gt; in as the &lt;code&gt;thisArg&lt;/code&gt; of &lt;code&gt;bind&lt;/code&gt; on the &lt;code&gt;setTimeout&lt;/code&gt; function, because in this case, &lt;code&gt;this&lt;/code&gt; is equal to the &lt;code&gt;bob&lt;/code&gt; object itself. (We could just as easily have passed in &lt;code&gt;bob&lt;/code&gt;, but often, we will see code that uses &lt;code&gt;this&lt;/code&gt; in cases such as this instead of the name of the object.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>jsfundamentals</category>
    </item>
    <item>
      <title>"Non-technical" is not a thing.</title>
      <dc:creator>Sean Ryan </dc:creator>
      <pubDate>Sun, 07 Apr 2019 23:29:27 +0000</pubDate>
      <link>https://dev.to/seanpryan/non-technical-is-not-a-thing-39k6</link>
      <guid>https://dev.to/seanpryan/non-technical-is-not-a-thing-39k6</guid>
      <description>&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fitzmeo53kp5pstw0eq5r.jpeg" 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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fitzmeo53kp5pstw0eq5r.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TL;DR: “Non-technical” is a false and limiting label that keeps people from discovering rewarding things.&lt;/p&gt;




&lt;p&gt;For the majority of my life, I considered myself a card carrying member of right-brained, reading-and-writing-oriented people club. I've played music my whole life, and I majored in History at a liberal arts college where I did not take a single math or science class.&lt;/p&gt;

&lt;p&gt;All of my professional experience since college has been under the marketing and communications umbrella. When I moved to New York, my first gig was at a PR firm where I wrote spammy emails to journalists. Not loving that, (enormous understatement), about six months later, I started looking around for jobs again, and eventually started working at a market research firm. &lt;/p&gt;

&lt;p&gt;For the last three years, I’ve been heading up digital marketing for a professional association that serves mainly research and academic computer scientists, and it was here that I realize something that for the entirety of my right brained existence seemed completely impossible: I like to code. &lt;/p&gt;

&lt;p&gt;It started innocently enough. I have to touch up a little HTML when I update our website content, and one day while doing this, I thought I’d dig a little deeper. I started doing some front-end web development tutorials on Team Treehouse, and I was hooked. I kept digging. Over the last year or so, I’ve worked on a few projects, worked through a few Udemy classes on front-end development, went to some JavaScript meetups in New York.&lt;/p&gt;

&lt;p&gt;I’ve been able to build a good foundation of knowledge on my own, but recently I realized that I wanted more structure and accountability, so after researching a few online bootcamp options (quitting my job to go full time wasn’t really on the table), I decided to go with Bloc.&lt;/p&gt;

&lt;p&gt;I want to be a web developer. I want to make a career out of making great products and continuously learning new technologies and skill sets, and I want to be around other people are also drawn this type of work. I knew this intellectually before I started Bloc, but now it feels much more real and attainable.&lt;/p&gt;

&lt;p&gt;I’ve come to realize that the tech industry, and more specifically the web development community within it, is not full of lonely, isolated weirdos who like staring at their computer screens more than being with other people. It’s full of creative and supportive individuals who love to learn and build things, and help other people do the same while they’re at it.&lt;/p&gt;

&lt;p&gt;One of the reasons why it took me so long to understand this has to do with a limitation in the way humans make sense of the world. Things are complicated, so naturally, people take some shortcuts to help make sense of it all. One of these shortcuts is to put people and things into categories that, more often than not, are split down the middle by a giant wedge. This applies to how we talk about the pursuits in life that others that are likely to be good at or not so good at, and how we apply words like “technical” or “creative” to the types of people who would excel at things like programming and writing or playing music. &lt;/p&gt;

&lt;p&gt;It’s important for people with educational and professional leverage to think about how hoarding students and employees into groups prevents people on both sides of the divide from discovering enriching and rewarding experiences. When educators and policy makers think about how to close the skills gap in technology, it’s not enough to simply bring more STEM programs to schools — we need to think about how to talk about sciences in a way that makes them feel less cold and distant to people who have already been told that their talents lie elsewhere. &lt;/p&gt;

&lt;p&gt;To those of you firmly entrenched in the "creative" camp: sit down with a tutorial on front-end web development and see what you think. You may find it exciting and rewarding, just like I did. And to those in technical town: pick up an instrument, write a play, or make some origami. Our schools and jobs don't often encourage us to venture to the other sides of our brains, so we have to be proactive. &lt;/p&gt;

</description>
      <category>selftaught</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
