Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ECMAScript 6's arrow function to a regular function [duplicate]

I have the following arrow function

if( rowCheckStatuses.reduce((a, b) => a + b, 0) ){}

rowCheckStatuses is an array of 1's and 0's, this arrow function adds them all up to produce a number. This number acts as a boolean to determine whether or not there is at least one "1" in the array.

The issue is, I don't really understand how arrow functions work, and my IDE thinks it's bad syntax and refuses to check the rest of my document for syntax errors.

How would I go about converting this to a regular function to alleviate both issues?

like image 841
snazzybouche Avatar asked Nov 30 '22 09:11

snazzybouche


1 Answers

An arrow function can usually be converted by replacing

(<args>) => <body>

with

function(<args>) { return <body>; }

So yours would be

rowCheckStatuses.reduce(function(a, b) { return a + b; }, 0)

There are exceptions to this rule so it's important that you read up on arrow functions if you want to know all of the differences. You should also note that arrow functions have a lexical this.

like image 100
Mulan Avatar answered Dec 04 '22 07:12

Mulan