Tag: 箭头函数

Express导出类中的箭头函数 – 意外的令牌

我有我的类,我输出和快递和节点使用的文件。 我想使用箭头function,这是一个例子: class MyClass { myFunc(arg) { console.log(arg); } myArrowFunc = (arg1, arg2) => { console.log(arg1); console.log(arg2); }; } module.exports = { MyClass } 一切工作正常之前添加myArrowFunc。 之后,运行应用程序后,我有一个错误: myArrowFunc = (arg1, arg2) => { ^ SyntaxError: Unexpected token = 我的节点版本是9.x所以它应该工作…或者我忘了一些事情。

如何从箭头function得到这个?

我在节点中使用mongoose库,并遇到了一个问题。 当使用pre hook保存时,我不能使用箭头函数将文档的引用作为“this”。 我被build议使用“function”的声明,但有一个我必须遵循的风格指南。 有没有办法在使用箭头function时获取“this”的对象?

asynchronous函数或async =>导出默认值时?

export default async function () { }; 要么 export default async () => { }; 导出默认函数时首选哪一个,为什么?

箭头函数与函数声明/expression式:它们是等价的还是可交换的?

规范性问题如果在使用箭头函数replace函数声明/expression式后发现有关问题的问题,请将其作为该函数的副本closures。 ES2015中的箭头function提供更简洁的语法。 我现在可以用箭头函数replace我所有的函数声明/expression式吗? 我需要注意什么? 例子: 构造函数 function User(name) { this.name = name; } // vs const User = name => { this.name = name; }; 原型方法 User.prototype.getName = function() { return this.name; }; // vs User.prototype.getName = () => this.name; 对象(文字)方法 const obj = { getName: function() { // … } }; // vs const […]

在node.js中的箭头函数上下文

使用箭头函数的MDN文档中的以下示例,位于https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions function Person(){ this.age = 0; setInterval(() => { this.age++; // |this| properly refers to the person object }, 1000); } var p = new Person(); 当我复制/粘贴该代码到node.js 0.12 node –harmony , this.age++; 行似乎不是指Person上下文,而是指setInterval上下文。 将console.log(this)添加到callback似乎证实了这一点。 当我使用其他es6-> es5 transpilers时,它一直按预期工作。 这是在node.js中的错误? 我错过了什么吗? 编辑:也许这是原因? V6中的ES6箭头函数词法 不同的是,他们正在讨论Chrome,而这个问题是关于Node.js的。 根据http://kangax.github.io/compat-table/es6/#arrow_functions,他们有不同级别的ES6支持,即使他们都使用V8。

在Ubuntu下,箭头函数不能在节点–harmony中工作

我试图在Ubuntu 14.04下使用node v0.10.33箭头函数(我正在使用–harmony标志运行节点),但是我得到这个错误: console.log( [1,2,3,4].map(x => x*x) ); ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3

ES6立即调用箭头function

为什么在Node.js控制台(在4.1.1和5.3.0中testing)中工作,但在浏览器中不工作(在Chrome中testing)? 这个代码块应该创build并调用一个loggingOk的匿名函数。 () => { console.log('Ok'); }() 另外,虽然上述在Node中工作,但这不起作用: n => { console.log('Ok'); }() 也不是这样: (n) => { console.log('Ok'); }() 奇怪的是,当参数被添加时,它实际上会在立即调用的部分抛出一个SyntaxError 。