什么=>在节点js中表示[重复]

时间:2021-05-02 16:58:59

This question already has an answer here:

这个问题在这里已有答案:

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

我正在学习节点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

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

2 个解决方案

#1


22  

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

它不是节点独有的,它是ES6 Arrow函数表达式

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 this value.

这两个例子的主要区别在于第一个例子是词汇上绑定了这个值。

#2


1  

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');
});

#1


22  

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

它不是节点独有的,它是ES6 Arrow函数表达式

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 this value.

这两个例子的主要区别在于第一个例子是词汇上绑定了这个值。

#2


1  

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');
});