What does the "=" operator mean?

15

I was seeing some solutions in JavaScript and in one case I saw this command line: return args.reduce((s, v) => s + v, 0); . But I do not know what the => operator means.

What's his role?

    
asked by anonymous 21.02.2016 / 01:09

2 answers

18

Known as Arrow functions . A Arrow function is exactly like a normal callback / function, but less verbose and instance references like this are taken from the "surroundings" (which avoids .bind() or those var that=this ).

Then:

var numbers = [1,2,3];
squares = numbers.map(x => x * x);

which is equivalent to:

squares = numbers.map(function (x) { return x * x });

I do not want to write an extensive answer with all the details because this is redundant. There is so much about it out there that it is not worth the effort. As an example, here is EXCELLENT content about it:

link

    
21.02.2016 / 01:18
8

This is a lambda function, or as it is usually called, arrow function . It's a anonymous function with a simpler syntax. Only available from EcmaScript 6.

The parentheses on the left are the parameters and what is on the right is the body of the function.

    
21.02.2016 / 01:14