简体   繁体   English

命名内联函数有什么优势?

[英]What advantage is there to name an inlined function?

In a eg. 在例如。 a callback, what is the difference between an a named inline function and an anonymous inline function and? 回调,命名内联函数和匿名内联函数之间有什么区别?

I think it increases the readability, but are there other advantages? 我认为它增加了可读性,但还有其他优点吗?

Eg. 例如。

element.addEventListener("load", function onLoad() {
  // execute code
});

vs.

element.addEventListener("load", function() {
  // execute code
});

Edit: I guess I also can do this with a named inline function 编辑:我想我也可以用命名的内联函数来做到这一点

...
element.addEventListener("load", function onLoad() {
  // execute code ...
  element.removeEventListener("load", onLoad);
});

You can make it recursive: 你可以使它递归:

document.addEventListener('mousemove', function stackOverflow() {
    stackOverflow();
});

You can't do that with anonymous inline functions. 您无法使用匿名内联函数执行此操作。 Not as easily, at least (there's arguments.callee , but it shouldn't be used anymore). 不是那么容易,至少(有arguments.callee ,但它不应该再使用了)。

Well, the only difference is the name property of the function will be blank string in anonymous function. 好吧,唯一的区别是函数的name属性将是匿名函数中的空字符串。 If you keep the function onLoad() instead of anonymous function, then the property onLoad.name will contain the string 'onLoad'. 如果保留function onLoad()而不是匿名函数,则属性onLoad.name将包含字符串'onLoad'。 The name property is an extension of the language (Its not part of an ECMA standard.) name属性是语言的扩展(它不是ECMA标准的一部分。)

The name property is useful, when using debuggers such as Firebug, or when calling the same function recursively from itself; 当使用诸如Firebug之类的调试器时,或者从自身递归调用相同的函数时,name属性很有用; otherwise you can just skip it. 否则你可以跳过它。

It's useful whenever you want to reference the function. 只要您想引用该功能,它就很有用。

Along with recursion ( as previously mentioned ), you can also bind data directly to the function (since functions are first class objects, right?). 除了递归( 如前所述 ),您还可以将数据直接绑定到函数(因为函数是第一类对象,对吧?)。 A useful application of this technique could include memoization where you could save previously computed values by your function to prevent recomputing the values again later. 此技术的一个有用应用可能包括memoization ,您可以通过函数保存以前计算的值,以防止以后再次重新计算值。

A simple counter – JSFiddle 一个简单的计数器 - JSFiddle

button.addEventListener("click", function fn() {
  fn.counter = fn.counter || 0;
  console.log(++fn.counter);
});

It is basically used to improve the stack traces in the Error objects; 它主要用于改善Error对象中的堆栈跟踪; So the in the stack trace of an error you would see the actual name of the function instead of the (anonymous functions) string. 因此,在错误的堆栈跟踪中,您将看到函数的实际名称而不是(匿名函数)字符串。

Bare in mind, though, that there are some scoping and compatibility issues. 但是,请记住,存在一些范围和兼容性问题。 Some JavaScript environments represent the scope of the named functions expressions as an instance of Object; 一些JavaScript环境将命名函数表达式的范围表示为Object的实例; that is, it inherits properties from the Object.prototype. 也就是说,它从Object.prototype继承属性。 So you can face a problem like this: 所以你可以面对这样的问题:

var constructor= function() { return null; };
var f = function() { return constructor() };
f(); 

The last line will return {} in some ES3 JavaScript environments and not null. 最后一行将在某些ES3 JavaScript环境中返回{},而不是null。 Thankfully, this issue is not present in ES5. 值得庆幸的是,ES5中没有此问题。

If you want to make it recursive you can alternatively do the following: 如果您想使其递归,您可以选择执行以下操作:

var recurs = function(){
    recurs();
} 

document.addEventListener('mousemove', recurs);

One more thing to note is the name of an inline function (or named function expression) is only visible within the function itself. 还有一点需要注意,内联函数(或命名函数表达式)的名称仅在函数本身中可见。

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

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