From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understanding this in arrow functions

Understanding this in arrow functions - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Understanding this in arrow functions

- In the previous lesson we reviewed how Arrow Functions can make our code more readable. In addition to this, though, arrow functions help us to deal with the scope of this in our JavaScript code. Let's take a look at an example of the old way and then we'll update this to the new syntax. First, I'll create a person object. The person object is going to have a first name and then it'll have an array of hobbies. So we'll add a few hobbies for this person to do. Then we're going to add print hobbies as a method for this object. Print hobbies is going to iterate over these hobbies and it's going to add a little string to our console. So the string will just pass back the values and then we'll be able to see what this person likes to do. Once I call person.print hobbies, we see sort of a weird output here. It says undefined likes to bike, hike, and ski. Now I thought if we use this, we will be able to access properties that are…

Contents