I do not get = () => {


#1

Is this something new in JS?
const pressPowerButton = () => {

}

It is not the same when I write:
const pressPowerButton () {

}


#2

It is called, arrow function syntax.

Standard form…

var pressPowerButton = function () {

};

or,

function pressPowerButton() {

}

Arrow form…

const pressPowerButton = () => {

};

Arrow function syntax is intended for use in functional programming.

We cannot use the syntax in object methods, and neither does it have the typical context objects of the imperative form (function()), this and arguments.

Some basic rules that apply to the syntax:

  1. Optional return keyword (conditions apply);
  2. optional parens on single parameter;
  3. optional curly braces (conditions apply).

Being as they are intended for functional programming, it’s in those roles we can make best use of them. Consider,

 > [1,2,3,,5,6,,8,9,,10].filter(x => x)
<- Array [ 1, 2, 3, 5, 6, 8, 9, 10 ]

The above returned only values that are truthy, as does the following…

 > [1,2,3,0,5,6,,8,9,0,10].filter(x => x)
<- Array [ 1, 2, 3, 5, 6, 8, 9, 10 ]

The equivalent to this in imperative form is,

 > [1,2,3,0,5,6,,8,9,0,10].filter(function (x) { return x && true })
<- Array [ 1, 2, 3, 5, 6, 8, 9, 10 ]

Clearly the arrow syntax wins out in this example.

Arrow functions are more like lambdas than regular functions. Consider this Python example of the same nature:

>>> list(filter(lambda x: x, [1,2,3,0,5,6,0,8,9,0,10]))
[1, 2, 3, 5, 6, 8, 9, 10]
>>> 

See the similarity?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.