• 请不要在回答技术问题时复制粘贴 AI 生成的内容
sunjourney
V2EX  ›  程序员

一个关于 Babel 的转译问题

  •  
  •   sunjourney · Aug 26, 2016 · 3592 views
    This topic created in 3729 days ago, the information mentioned may be changed or developed.

    这段代码

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

    会被转译为

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

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

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

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

    9 replies  •  2016-08-26 15:34:50 +08:00
    messense
    messense
    Aug 26, 2016
    1
    匿名函数不方便 debug
    szharrydev
    szharrydev
    Aug 26, 2016
    2
    给匿名函数加了个方法名而已 。。。 调用还是一样 。。。
    yyfearth
    yyfearth
    Aug 26, 2016
    3
    只有这样 function 的 name 属性才会是 method
    adeweb
    adeweb
    Aug 26, 2016
    4
    3 楼正解。
    sunjourney
    sunjourney
    OP
    Aug 26, 2016
    5
    @yyfearth 怎么拿到这个 function name ? test.method.name 用我的写法也是 method
    TomIsion
    TomIsion
    Aug 26, 2016
    6
    Mark 下
    morethansean
    morethansean
    Aug 26, 2016❤️1
    7
    @sunjourney https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name

    里面提到实现了 ES6 functions 的浏览器能推断出 function name.
    你的测试环境是最新的 Chrome 吧?
    sunjourney
    sunjourney
    OP
    Aug 26, 2016
    8
    @morethansean 感谢,正解!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2241 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:41 · PVG 21:41 · LAX 05:41 · JFK 08:41
    ♥ Do have faith in what you're doing.