The arrow functions are simply shortcuts to assignable functions or callbacks to maintain the context in which they are called without making explicit% .bind
. For example:
class A {
constructor () {
this.name = 'Letra A';
}
foo () {
setTimeout(function () {
console.log(this.name);
}, 3000);
}
}
new A().foo();
The previous example shows one of the problems thanks to which this new notation for functions was proposed. The above code prints undefined
because this
refers to window
and not% A
.
The above is solved in ES5 as follows:
setTimeout(function () {
console.log(this.name);
}.bind(this), 3000);
As of ES6, with the arrival of the arrow functions, the solution is simpler:
setTimeout(() => console.log(this.name), 3000);
Answering your question, the way to iterate over an arrangement is the same using ES6:
let names = ['John', 'Martha', 'Leslie'];
names.forEach(name => console.log(name));