为什么要将函数声明分配给一个命名变量? [英] Why would I assign a function declaration to a named variable?

查看:162
本文介绍了为什么要将函数声明分配给一个命名变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:它不是将函数声明赋值给命名变量 - 检查接受的答案。




在阅读Paul Irish的 infinitescroll jquery插件代码,我偶然发现了以下模式:

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

这样做的好处不在于:

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

解决方案

这个功能的好处在于函数有一个实际的名字。在你的第二个版本中,属性有一个名字,但是这个函数没有。赋予函数实际名称可以帮助您的工具帮助您(调用堆栈列表,断点列表等)更多信息: 匿名



缺点是它在一些意想不到的结果破坏的JavaScript引擎,就像IE8及更早版本中的引擎。在IE8及更早版本中,Paul Irish的版本在两个截然不同的时间创建了两个独立的函数

一>。但是,除非您保留并使用对它们的引用,并且期望它们是相同的函数(例如,在挂钩和取消挂钩事件处理程序时),否则它不是真正的问题。鉴于这是保罗,我猜他肯定不会那样做。






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



为了完整性:

  //这是一个函数声明 - 请注意,它不是右手
//值,例如,我们不要立即使用它的结果(通过
//赋值,属性初始值设定项,调用它,或者将它传递到
//作为参数的函数 - 这些都不是)。
//
//声明在进入范围时发生(不是作为逐步
//步代码的一部分)。该函数的名称被添加到其声明
的作用域中。声明在分支内部是不合法的(`if`,`try / catch`,
//`for`等),但是一些引擎会将它们重写为
的表达式//如果你这样做。其他人不会,他们只会总是声明
//函数,无论代码是否到达。所以不要这样做。
函数foo(){
}

//这些都是匿名函数表达式。
//表达式中的函数没有名字,尽管一些调试器非常聪明
//关于查看表达式和(在哪里可以)列出
//伪类型该函数的名称。其他人不那么聪明,
//这就是为什么我避免匿名函数。
//
//表达式在分步代码到达时发生。
var f = function(){};
var obj = {
prop:function(){}
};
doSomethingCoolWithAFunction(function(){});
(function(){})(); //立即调用
!function(){}(); //立即调用
〜function(){}(); //立即调用它,有几个变体

//这些都是* named *函数表达式。
//
//由于它们是表达式,它们在
// //一步一步的代码中到达时发生。该函数的名称不会被添加到包含
//范围(除了具有错误的引擎)。
//
//这些与上面的例子相同,只是带有一个名称。没有其他变化。
var f = function foo(){};
var obj = {
prop:function foo(){}
};
doSomethingCoolWithAFunction(function foo(){});
(function foo(){})(); //立即调用
!function foo(){}(); //立即调用
〜function foo(){}(); //立即调用它,有几个变体


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:

...
_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

The disadvantage to it is that it has unexpected results in some broken JavaScript engines, like the one in IE8 and earlier. In IE8 and earlier, Paul Irish's version creates two separate functions at two completely different times. 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

这篇关于为什么要将函数声明分配给一个命名变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆