V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sunjourney
V2EX  ›  程序员

一个关于 Babel 的转译问题

  •  
  •   sunjourney · 2016-08-26 11:36:11 +08:00 · 2401 次点击
    这是一个创建于 2772 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这段代码

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

    会被转译为

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

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

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

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

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

    里面提到实现了 ES6 functions 的浏览器能推断出 function name.
    你的测试环境是最新的 Chrome 吧?
    sunjourney
        8
    sunjourney  
    OP
       2016-08-26 15:24:45 +08:00
    @morethansean 感谢,正解!!
    sinalvee
        9
    sinalvee  
       2016-08-26 15:34:50 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2868 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:19 · PVG 19:19 · LAX 04:19 · JFK 07:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.