Prev | Table of contents | Next
const fn = () => value;
// equal to const fn = function() { return value; };
const sum = (first, second) => first + second;
const addTwo = value => value + 2;
// equal to const addTwo = function(value) { return value; };
we have to provide "return" statement when using block:
const fn = param => {
const something = getSomething();
const preparedSomething = prepareSomething(something);
return modifySomething(preparedSomething);
};
to return object in simple arrow function (because {} is block):
const fn = param => ({ param: param });
Arrow functions don't have "name" property and context ("this" links to closest parent component with context):
function Timer
{
this.seconds = 0;
setInterval(
() => this.seconds++,
1000
);
}
const timer = new Timer();
setTimeout(() => console.log(timer.seconds), 4200); // 4