一个关于 Babel 的转译问题

2016-08-26 11:36:11 +08:00
 sunjourney

这段代码

let test = {
  method () {
    // ...
  }
}

会被转译为

"use strict";

var test = {
  method: function method() {
    // ...
  }
};

那么 function method () 有方法调用吗?如果没有,何不转译成这样:

"use strict";

var test = {
  method: function () {
    // ...
  }
};

严格模式下,就不要说什么 callee 了

2435 次点击
所在节点    程序员
9 条回复
messense
2016-08-26 11:44:01 +08:00
匿名函数不方便 debug
szharrydev
2016-08-26 11:45:28 +08:00
给匿名函数加了个方法名而已 。。。 调用还是一样 。。。
yyfearth
2016-08-26 12:29:07 +08:00
只有这样 function 的 name 属性才会是 method
adeweb
2016-08-26 12:39:43 +08:00
3 楼正解。
sunjourney
2016-08-26 13:54:36 +08:00
@yyfearth 怎么拿到这个 function name ? test.method.name 用我的写法也是 method
TomIsion
2016-08-26 14:22:11 +08:00
Mark 下
morethansean
2016-08-26 14:36:02 +08:00
@sunjourney https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

里面提到实现了 ES6 functions 的浏览器能推断出 function name.
你的测试环境是最新的 Chrome 吧?
sunjourney
2016-08-26 15:24:45 +08:00
@morethansean 感谢,正解!!
sinalvee
2016-08-26 15:34:50 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/301885

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX