将ECMAScript 6的arrow函数转换为常规函数 [英] Converting ECMAScript 6's arrow function to a regular function

查看:56
本文介绍了将ECMAScript 6的arrow函数转换为常规函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下箭头功能

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

rowCheckStatuses是一个1和0的数组,此箭头功能将它们全部加起来以产生一个数字.此数字用作确定数组中是否至少有一个"1"的布尔值.

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.

问题是,我不太了解箭头函数的工作原理,我的IDE认为这是错误的语法,因此拒绝检查文档的其余部分是否存在语法错误.

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?

推荐答案

您可以将其重构为:

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

不需要初始累加器(除非您期望数组有时为空),它可能是:

The initial accumulator isn't necessary (unless you expect the array to be empty sometimes), it could be:

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

此数字用作确定数组中是否至少有一个"1"的布尔值

This number acts as a boolean to determine whether or not there is at least one "1" in the array

使用起来可能更快(更清晰):

It might be faster (and clearer) to use:

if( rowCheckStatuses.some(function(a){return a == 1}))

,如果 rowCheckStatuses 中有1,则返回 true ,并在遇到后立即返回.另一种选择是 indexOf :

which will return true if there are any 1s in rowCheckStatuses and will return as soon as one is encountered. Another alternative is indexOf:

if( rowCheckStatuses.indexOf(1) != -1)

很多选择.

这篇关于将ECMAScript 6的arrow函数转换为常规函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆