匿名函数和node.js中的=>符号有什么区别?

我只是想用node.js读取一个文件。

我曾经使用这个符号:

fs.readFile('/etc/passwd', function(err, data) { if (err) throw err; console.log(data); }); 

Node.js的文档提供了以下代码:

 fs.readFile('/etc/passwd', (err, data) => { if (err) throw err; console.log(data); }); 

他们之间有什么区别?

  • 箭头function是ES6中的新function,因此它们在旧版浏览器中不受支持 。

  • 箭头function有this词汇。

     this.foo = 'bar'; baz(function() { this.foo // probably undefined }); baz(() => { this.foo // == 'bar' }); 

=>是es6符号。 箭头function始终是匿名的。 你给出的两段代码的function是一样的。

第二个是ECMA 6,第一个比较老。 没有区别。