简体   繁体   English

Babel箭头功能编译成ES5

[英]Babel arrow function compilation into ES5

Using Babel I've noticed something a little weird. 使用巴别塔我注意到一些有点奇怪的东西。

Shouldn't const app = () => {} be equal to var app = function() {} ? const app = () => {}应该等于var app = function() {}

Babel returns var app = function app() {} . Babel返回var app = function app() {}

No, babel is correct as an arrow function assigned to a var should in theory have a name property equivalent to the name of that var to assist in stack traces and reflection. 不,babel是正确的,因为分配给var的箭头函数理论上应该具有与该var的名称相当的name属性,以帮助堆栈跟踪和反射。 Check out this link for more info. 查看链接以获取更多信息。 Quick summary in case it goes stale: 如果它变得陈旧,请快速摘要:

The name property of a function is created at declaration time. 函数的name属性在声明时创建。 The name property of a function expression is inferred from the name binding: 函数表达式的name属性是从名称绑定推断出来的:

var foo = function() {};
console.log(foo.name); // foo

Arrow functions have the same behavior: 箭头函数具有相同的行为:

var foo = () => {};
console.log(foo.name); // foo

Since this didn't actually get standardized until ES 2015/ES 6 babel has to actually add the name to create a named function expression : 因为在ES 2015 / ES 6 babel必须实际添加名称以创建命名函数表达式之前,这实际上没有标准化:

var foo = function foo() {}; // notice it's function *foo* now

to support legacy environments. 支持传统环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM