0

I recently came across a bit of javascript in my quest to self learn the language which is a clever way of turning the string 'code wars' to C.Wars and 'Barack Hussain obama' to B.H.Obama. My question is on the use of the =>. I could not find details on it. Here is the snippet:

n.split(' ').map((v, i, a) => v.charAt(0).toUpperCase() + (i == a.length - 1 ? v.slice(1) : '.')).join('')

the syntax of map in javascript is:

arr.map(callback[, thisArg])

So would it be right to assume that the => is injecting an anonymous callback function with the parameters v, i and a. From the documentation I see that the callback function for map must contain three arguments:

currentValue
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array map was called upon.

Is there any where I can find about the => syntax and its other uses or can someone explain it further to me?

A

amadain
  • 2,236
  • 2
  • 29
  • 51
  • 1
    ECMAScript 6 [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Hacketo Jul 22 '15 at 07:50

1 Answers1

1

That is an arrow function. It represent an anonymous function. It came with the EcmaScript 6 standard.

It does mostly the same as a regular function() {<something>} except it carries on with the "this" from the outer scope. It does not have its own "this". This means you do not need to bind "this" into the callback function anymore.

Here you can find further information.

magnudae
  • 1,214
  • 2
  • 13
  • 32