In es2015, `const func = foo => bar` makes `func` a named function, how do you bypass this?

后端 未结 2 733
走了就别回头了
走了就别回头了 2021-01-24 10:05

Is there any way to get around this behavior?

> foo => bar;
[Function]
> const func = foo => bar;
undefined
> func
[Function: func]
2条回答
  •  我在风中等你
    2021-01-24 10:42

    This is not related to ECMAScript.

    It's true that, when you assign an anonymous function a variable, the name of the function will be set to the name of the variable (thanks @loganfsmyth for the correction).

    Since name is a configurable property, you could delete it. However, that would be useless, because name is unrelated.

    For example, Firefox hasn't implemented naming anonymous functions as the variables yet, so name is the empty string in your example. But the console still displays function func() when you log the function.

    In fact, the "problem" is that browsers want to make debugging code easier, so their console infers a name (not necessarily the name) for the function.

    Engines can infer function names gives this example of inference:

    Anonymous functions no longer need to be named for debugging

    function f() {}            // display name: f (the given name)
    var g = function () {};    // display name: g
    o.p = function () {};      // display name: o.p
    var q = {
      r: function () {}        // display name: q.r
    };
    function h() {
      var i = function() {};   // display name: h/i
      f(function () {});       // display name: h/<
    }
    var s = f(function () {}); // display name: s<
    

    The consoles of the browsers are not standardized by ECMAScript, so you can't modify this behavior.

提交回复
热议问题