Tag: arrow functions

你如何创build一个命名的asynchronous箭头function?

目前,我有这样的代码: async function getConnection(){ // logic here… } 为了使其与我的代码库的其余部分保持一致,我想将其更改为箭头函数。 我试过async getConnection () => { … }但似乎没有工作。 什么是正确的方法来做到这一点?

在节点0.11中使用ES6箭头函数w / Foo.prototype

我得到了我在使用原型扩展中使用箭头函数的意外行为。 function ES6Example(){} ES6Example.prototype.foo = function(bar){ return ((baz) => { console.log(this) this.bar = baz })(bar) } var es6Example = new ES6Example es6Example.foo('qux') console.info(es6Example.bar) 上面的代码导致全局上下文被打印出来, es6Example.bar也是未定义的。 这是旧的行为。 基于我在MDN中看到的文档,我期望这个绑定到实例。 我正在使用和谐标志使用节点v0.11.15运行上面的代码。 请注意,下面的工作: function ES6Example(){ this.foo = baz => { this.bar = baz } }

为什么'this'在箭头函数中被引用时返回'undefined',但是在匿名函数中被调用时不会被返回?

我正在使用node.js v6.7.0,同时声明一个引用'this'的对象,如果它在一个箭头函数内部,则返回undefined,但当它在一个普通的匿名函数内部时,它会返回对象本身(这就是我想) 例如 let obj = { key: 'val', getScopeWithArrow: () => {return this;}, //returns undefined getScopeWithAnonymous: function() {return this;} //returns the object properly }