Javascript中的等号和大于号(=>)是什么意思? [英] What does it mean equal and greater-than sign (=>) in Javascript?

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

问题描述

在Meteor中Whatsapp示例项目是使用=>的文件,但我的WebStorm IDE将其检测为错误。我找不到任何关于此语法的文档。

In Meteor Whatsapp example project is file where "=>" is used, but my WebStorm IDE detect it as error. I can't find any docs about this syntax.

chats.forEach( chat => {
  let message = Messages.findOne({ chatId: { $exists: false } });
  chat.lastMessage = message;
  let chatId = Chats.insert(chat);
  Messages.update(message._id, { $set: { chatId: chatId } })
});

bootstrap.js文件的GitHub存储库在这里

什么是=>?

推荐答案

我实际上是要对这个问题进行投票,但如果你还不知道它叫什么,谷歌搜索答案就太难了。正如你在评论中的链接中看到的那样,这是一个胖箭头函数(有时简称为箭头函数)。

I was actually about to downvote this question, but googling the answer proved surprisingly difficult if you don't already know what its called. As you can see in the links in the comments, that's a fat arrow function (sometimes referred to as just an arrow function).

有一些令人困惑的箭头功能方面,所以我会点击一些亮点:

There are some confusing aspects of arrow functions, so I'll hit some highlights:

普通函数有一个这个指针设置取决于上下文:用 new 调用的函数将它设置为新创建的对象,被称为方法的函数将它绑定到调用该方法的对象,否则绑定到 undefined 或全局对象(取决于'严格模式'编译指示) ,当然可以用 Function.prototype.bind 等设置。

Normal functions have a this pointer set depending on the context: functions called with new have it set to the newly-created object, functions called as methods have it bound to the object the method was called from, its otherwise bound to undefined or the global object (depending on the 'strict mode' pragma), and can of course be set with Function.prototype.bind et al.

但箭头函数没有绑定对于运行时创建的 this 指针(也不能通过 Function.prototype.bind 指定),这意味着它像任何其他var一样,通过范围链分辨率从词法上查找。 MDN文章在这一点上充其量令人困惑(参见上面的链接)。

But arrow functions have no binding for the this pointer created by the runtime (nor can it be specified via Function.prototype.bind), meaning it gets lexically looked up through scope chain resolution just like any other var. The MDN article is at best slightly confusing on this point (see link above).

此外,箭头函数有一个隐式返回,返回值将自动成为最后一次评估函数体中的表达式。

Additionally, arrow functions have an implicit return, the return value will automatically be the last evaluated expression in the function body.

箭头函数没有参数伪造数组。您可以使用ES 6 rest参数。

Arrow functions have no arguments psuedo-array. You can use ES 6 rest parameters instead.

对于arity 1的函数,参数周围的parens可能会被省略。

For functions of arity 1, the parens around the parameter may be omitted.

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

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