Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 1.24 KB

05-arrow-functions.md

File metadata and controls

45 lines (35 loc) · 1.24 KB

Prev | Table of contents | Next

Arrow functions

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

Prev | Table of contents | Next