简体   繁体   English

为什么要为命名变量分配函数声明?

[英]Why would I assign a function declaration to a named variable?

Edit: it's NOT an assignment of a function declaration to a named variable - check the accepted answer. 编辑: 它不是对命名变量的函数声明的赋值 - 检查接受的答案。 Leaving title as it is because other people might make the same error as me. 保留标题,因为其他人可能会犯同样的错误。


While reading Paul Irish's infinitescroll jquery plugin code, I stumbled again and again over the following pattern: 在阅读Paul Irish的infinitescroll jquery插件代码时,我偶然发现了以下模式:

...
_create : function infscr_create (options, callback) { /* ... */ },
...

What is the benefit of doing this instead of: 这样做的好处是什么,而不是:

...
_create : function (options, callback) { /* ... */ },
...

The benefit of that (which is called a "named function expression") is that the function has an actual name. 它的好处(称为“命名函数表达式”)是该函数具有实际名称。 In your second version, the property has a name, but the function doesn't. 在您的第二个版本中,该属性具有名称,但该功能没有。 Giving functions actual names helps your tools help you (call stack listings, breakpoint listings, etc.) More: Anonymouses anonymous 给函数实际名称可以帮助您的工具帮助您(调用堆栈列表,断点列表等)更多: Anonymousouses anonymous

The disadvantage to it is that it has unexpected results in some broken JavaScript engines, like the one in IE8 and earlier. 它的缺点是它在一些破坏的JavaScript引擎中有意想不到的结果,比如IE8和更早版本的引擎。 In IE8 and earlier, Paul Irish's version creates two separate functions at two completely different times . 在IE8及更早版本中,Paul Irish的版本在两个完全不同的时间创建了两个独立的功能 But it's not really a problem unless you keep and use references to both of them, and expect them to be the same function (for instance, when hooking up and unhooking event handlers). 但这并不是一个问题,除非你保留并使用对它们的引用,并期望它们是相同的函数(例如,当挂钩和取消挂钩事件处理程序时)。 Given it's Paul, I'm guessing he's being sure not to do that. 鉴于它是保罗,我猜他肯定不会这样做。


Re your question title: Note that it's not a function declaration , but you can be forgiven for thinking it is, as it looks almost exactly like one. 重新提问你的问题标题:请注意,这不是一个函数声明 ,但你可以原谅它,因为它看起来几乎就像一个。 :-) It's a function expression . :-)这是一个函数表达式 Function declarations and function expressions happen at completely different times, and have different impacts on the scope in which they're created. 函数声明和函数表达式发生在完全不同的时间,并且对它们的创建范围产生不同的影响。

Just for completeness: 只是为了完整性:

// This is a function declaration -- note that it's not a "right-hand
// value", e.g., we're not using the result of it immediately (via an
// assignment, a property initializer, calling it, or passing it into
// a function as an argument -- none of those).
//
// Declarations happen upon entry to the scope (not as part of step-by-
// step code). The function's name is added to the scope in which it's
// declared. Declarations are illegal inside branches (`if`, `try/catch`,
// `for`, etc.), but some engines will rewrite them as expressions for
// you if you do that. Others will not, they'll just always declare the
// function regardless of whether the code was reached. So don't do that.
function foo() {
}

// These are all anonymous function expressions. The function in the
// expression has no name, although some debuggers are pretty smart
// about looking at the expression and (where they can) listing a
// kind of pseudo-name for the function. Others are not that smart,
// which is why I avoid anonymous functions.
//
// Expressions happen when they're reached in step-by-step code.
var f = function() { };
var obj = {
    prop: function() { }
};
doSomethingCoolWithAFunction(function() { });
(function() { })(); // Call it immediately
!function() { }();  // Call it immediately
~function() { }();  // Call it immediately, there are a few variants

// These are all *named* function expressions.
//
// Since they're expressions, they happen when they're reached in the
// step-by-step code. The function's name is NOT added to the containing
// scope (except by engines with bugs).
//
// These are the same examples as above, but with a name. No other changes.
var f = function foo() { };
var obj = {
    prop: function foo() { }
};
doSomethingCoolWithAFunction(function foo() { });
(function foo() { })(); // Call it immediately
!function foo() { }();  // Call it immediately
~function foo() { }();  // Call it immediately, there are a few variants
  1. The function has a name, rather than being an anonymous function; 该函数有一个名称,而不是一个匿名函数; this shows up in debug traces, making debugging easier. 这显示在调试跟踪中,使调试更容易。
  2. The function can invoke itself using by calling infscr_create() 该函数可以通过调用infscr_create()来调用自身

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

相关问题 为什么要为变量分配一个函数而不是声明一个命名函数? - Why would you assign a function to a variable instead of declaring a named function? 为什么不能给具有相同名称的命名函数表达式中的变量赋值? - Why can’t I assign values to a variable inside a named function expression with the same name? 为什么我要从javascript函数返回命名函数(对象)? - why would I return a named function (object) from a javascript function? 为什么在此v​​ar声明中将此变量赋值给自身? - Why assign this variable to itself in this var declaration? 为什么函数声明不是变量声明时的声明 - Why is a function declaration not a statement while a variable declaration is 为什么变量声明不允许作为参数,但函数声明是? - Why is variable declaration not allowed as a parameter, but function declaration is? 如何在变量声明中使用匿名函数将字符串分配给变量? - How do I assign a string to a variable using an anonymous function in the variable declaration? 为什么要使用||在变量函数声明? - Why use || in variable function declaration? 在 Javascript 中,什么时候需要将命名函数分配给变量? - In Javascript, when is it necessary to assign a named function to a variable? 为什么要将此分配给另一个变量? - why would you assign this to another variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM