简体   繁体   English

向对象声明中添加函数名称有什么好处?

[英]What would be the benefit of adding a function name to an object declaration?

For instance, when I assign a function as a property of an object declaration, my loader for Webpack, which is babel-loader, will automatically add a function name? 例如,当我将函数分配为对象声明的属性时,我的Webpack加载器(即babel-loader)会自动添加函数名称吗? For instance, let's say I have the following compare function: 例如,假设我具有以下比较功能:

var utils = {
  compare: function (a, b) {
    if (parseFloat(a[0]) - parseFloat(b[0]) === 0) {
      return parseFloat(a[1]) - parseFloat(b[1]);
    }
    else {
      return parseFloat(a[0]) - parseFloat(b[0]);
    }
  }
}

Webpack will compile it to the following: Webpack会将其编译为以下内容:

var utils = {
  compare: function compare(a, b) {

    if (parseFloat(a[0]) - parseFloat(b[0]) === 0) {
      return parseFloat(a[1]) - parseFloat(b[1]);
    } else {
      return parseFloat(a[0]) - parseFloat(b[0]);
    }
  }
}

[compare: function (a,b) has been changed to compare: function compare (a,b)] [比较:功能(a,b)已更改为比较:功能比较 (a,b)]

It's just a good practice to name all your functions for debugging and recursion purposes. 为调试和递归目的命名所有函数只是一个好习惯。 Babel does that on the transpiled code with the loader, is not Webpack's work. Babel使用加载程序在已编译的代码上执行此操作,这不是Webpack的工作。

In the code you posted in the question there would indeed be little to no benefit. 在问题中发布的代码中,确实没有什么好处。 However, to recursively call an anonymous function you need to give it a name (at least in newer versions of javascript). 但是,要递归调用匿名函数,您需要给它命名(至少在较新版本的javascript中)。 This syntax has a name. 此语法具有名称。 It's called the named function expression . 它称为命名函数表达式

In older versions of javascript, the arguments object has a property called .callee that refers to the function. 在旧版本的javascript中, arguments对象具有一个称为.callee的属性,该属性引用该函数。 This could be used to recurse into an anonymous function: 这可以用于递归到匿名函数:

var sum = function(numbers){
    var n = numbers.pop();
    if (numbers.length) {
        return n + arguments.callee(numbers);
    }
    return n;
}

The arguments.callee property has been deprecated in ES5. ES5中不推荐使用arguments.callee属性。 Therefore in current (and newer) versions of javascript you need to use a named function expression to do recursion on an anonymous function: 因此,在当前(和更新)的javascript版本中,您需要使用命名函数表达式对匿名函数进行递归:

var sum = function s (numbers){
    var n = numbers.pop();
    if (numbers.length) {
        return n + s(numbers);
    }
    return n;
}

Note, that according to the specification, the name of a named function expression is private to the function itself. 注意,根据规范,命名函数表达式的名称对函数本身是私有的。 Therefore, in a non-buggy javascript interpreter/compiler the second example above should not create a function named s() outside of the anonymous function sum() . 因此,在非笨拙的javascript解释器/编译器中,上面的第二个示例不应在匿名函数sum()之外创建名为s()的函数。 However, some ES4 implementations (older browsers) were known to be broken and would create both s() and sum() . 但是,已知某些ES4实现(旧版浏览器)被破坏了,并且会同时创建s()sum() Apart from the name leak the named function expression syntax works in all browsers released in the last 10 years or so. 除了名称泄漏外,命名函数表达式语法还可以在最近十年左右的所有浏览器中使用。

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

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