Javascript => 在节点 js 中是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39328295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:28:47  来源:igfitidea点击:

What does => mean in node js

javascriptnode.jsecmascript-6

提问by deeveeABC

I am learning node js, and came across '=>' several times, however struggle to understand what this means.

我正在学习 node js,并=>多次遇到 ' ',但很难理解这意味着什么。

Here is an example:

下面是一个例子:

app.post('/add-item', (req, res) => {
  // TODO: add an item to be posted
});

Do we actually need this in the above example? A simple explanation would be helpful. Thanks

在上面的例子中我们真的需要这个吗?一个简单的解释会有所帮助。谢谢

回答by roberrrt-s

It's nothing node-exclusive, it's an ES6 Arrow function expression

它不是节点专有的,它是一个ES6 箭头函数表达式

app.post('/add-item', (req, res) => {
  // TODO: add an item to be posted
});

basically means:

基本上是指:

app.post('/add-item', function(req, res) {
  // TODO: add an item to be posted
});

The main difference between these two examples is that the first one lexically binds the thisvalue.

这两个示例之间的主要区别在于,第一个示例在词法上绑定了this值。

回答by Vishal Gupta

This is just a different way of writing an anonymous function:

这只是编写匿名函数的另一种方式:

$(document).ready(() => {
    console.log('Hello I am typescript');
});

is equivalent to JavaScript:

相当于 JavaScript:

$(document).ready(function(){
    console.log('Hello I am typescript');
});