Michael J. Stokes | Learning React ES6: Arrow Functions and Using 'This' Keyword


Today I got to learn how to use arrow functions in React ES6. From what I realized it that they are a much shorter syntax and typically simplifies the function scope making the `this` keyword more understandable. In ES5, the function scope was written like this:

function square (x) { return x * x; };

Today in ES6, I found myself writing my code using the new arrow functions like this:

const squareArrow = (x) => x * x;

The this keyword always confused me. Now I really get it how this works. It refers to either a function or an object and is no longer bound.

const user = {
name: ‘Mike’,
cities: [‘New York’, ‘Charlotte’, ‘Atlanta’],
printPlacesLived() {
return this.cities.map((city) => this.name + ‘ has lived in ‘ + city);
}
};

Not only from what I’m learning in React, but now after this, I’m beginning to really understand JavaScript a little more.



No Comments Yet

You can be the first to comment!


Leave a Comment






Previous Post : « | Next Post : »