Arrow functions came into existence with ES6. Since then they have been looked at as the fix all solution when running into problems with this
in our code. However it might not magically work all the time and this lesson is designed to show you how it really resolves this
. We first need to understand what lexical scope is. It references the author time of our code which can be confusing considering this
is runtime dependant. Let's look at all three of these examples to figure out how arrow functions work with this
.
While the last example is really great for the learning sake, in practice having the nested function is overkill IMHO. As an improvement, the setInterval could be passed a function where the call is made explicitly setInterval(() => { person.getName() }, 3000)
.
the nested function is overkill IMHO
For sure! Your code snippet is arguable the best way to write this code. As you said, the code I used in the example provides the best for learning purposes I think.