2012-07-05 22 views
4

编辑:它不是一个函数声明分配给一个命名变量 - 检查接受的答案。让标题保持原样,因为其他人可能会和我一样犯下同样的错误。为什么我要将一个函数声明分配给一个命名变量?


在阅读保罗爱尔兰的infinitescroll jQuery插件代码,我一次又一次地绊倒在以下模式:

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

是什么做的,而不是这个的好处:

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

回答

6

该功能(称为“命名函数表达式”)的好处是函数具有实际名称。在你的第二个版本中,属性有一个名字,但是该函数没有。让功能实际名称可以帮助你的工具可以帮助你(调用堆栈列表,断点列表等)更多:Anonymouses anonymous

缺点它是,它有一些破碎的JavaScript引擎意想不到的结果,像在IE8和更早。在IE8及更早版本中,Paul Irish的版本创建了two separate functions at two completely different times。但是,除非您保留并使用对它们的引用,并且期望它们是相同的函数(例如,在挂钩和取消挂钩事件处理程序时),否则它不是真正的问题。鉴于这是保罗,我猜他肯定不会那样做。


回复您的问题标题:请注意,这不是一个功能声明,但你可以原谅的思想是,因为它看起来几乎完全一样的。 :-)这是一个函数表达式。函数声明和函数表达式在完全不同的时间发生,并且对它们创建的范围有不同的影响。

只是为了完整性:

// 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 
+0

干杯,并感谢链接解释调用本身。 – timkg 2012-07-05 10:34:12

+0

@timkg:不客气,很高兴帮助。有趣的是,在IE8和更早版本中,用任何现代浏览器比较[这些测试](http://jsbin.com/awecoh)的结果。 :-) – 2012-07-05 10:41:49

5
  1. 函数都有一个名字,而不是一个匿名函数;这显示在调试跟踪中,使调试更容易。
  2. 该功能可以使用通过调用infscr_create()
+0

Upvoted简洁。 – timkg 2012-07-05 10:33:14

相关问题