gpt4 book ai didi

javascript - 为什么要将函数声明分配给命名变量?

转载 作者:搜寻专家 更新时间:2023-11-01 04:58:04 24 4
gpt4 key购买 nike

编辑:它不是将函数声明赋值给命名变量 - 检查已接受的答案。保留标题不变,因为其他人可能会犯与我相同的错误。


在阅读 Paul Irish 的 infinitescroll 时jquery 插件代码,我一次又一次地被以下模式绊倒:

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

这样做的好处是什么而不是:

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

最佳答案

这样做的好处(称为“命名函数表达式”)是该函数具有实际名称。在您的第二个版本中,属性 有名称,但函数没有。为函数提供实际名称有助于您的工具帮助您(调用堆栈列表、断点列表等)更多: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

关于javascript - 为什么要将函数声明分配给命名变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11342270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com