什么是=>意思是在JavaScript? (等于大于) [英] What does => mean in JavaScript? (equals greater than)

查看:153
本文介绍了什么是=>意思是在JavaScript? (等于大于)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr:what do => do?

tl;dr: what does => do?

我刚刚完成了解决问题的代码,并在查看其他人对该问题的常见回应,我一直看到: =>

I've just finished solving a problem on codewars and after looking at other peoples' common responses to the problem, I keep seeing this: =>

问题出在下面:

你有箭头箭,但有些已被损坏。箭头包含带有可选范围信息的箭头(不同类型的目标位于不同的范围),因此每个项目都是箭头。
您需要验证是否有一些好的选项,以准备战斗。 下面是一个示例数组,它是箭头箭头。

You have a quiver of arrows, but some have been damaged. The quiver contains arrows with an optional range information (different types of targets are positioned at different ranges), so each item is an arrow. You need to verify that you have some good ones left, in order to prepare for battle. The below is an example array which is the quiver of arrows.

anyArrows([
{range: 5}, 
{range: 10, damaged: true}, 
{damaged: true}
])

如果颤抖没有损坏的状态,这意味着它是新的。

If an arrow in the quiver does not have a damaged status, it means it's new.

这是一个我看到的示例,返回true或false,具体取决于是否有一个未损坏的箭头颤抖:

This is an example I saw which returns true or false, depending on if there is an undamaged arrow in the quiver:

function anyArrows(arrows){
  return arrows.some(a => !a.damaged);
}

现在,这比我的代码更短!我的矿井更基础:

Now, that was way shorter than my code! Mine was a lot more basic:

function anyArrows(arrows){
  for ( var i = 0 ; i < arrows.length ; i++ ){
    if ( arrows[i].damaged === false ) { return true; }
    else if (arrows[i].damaged === true) { return false; }
    else if (arrows[i].range === 0) { return false }
    else { return true; } 
  }
  if (arrows.length === 0) return false;
}

同样,问题是: = < / code>在这种情况下和一般情况下?

Again though, the question is: what does => do in this case and in general?

推荐答案

=> ; 是将箭头函数参数与正文分开的ES2015语法,例如(params)=> {/ * body * /}

=> is ES2015 syntax that separates arrow function parameters from body, e.g. (params) => { /* body */ }.

ArrowFunction:ArrowParameters => ConciseBody

这篇关于什么是=&gt;意思是在JavaScript? (等于大于)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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