jQuery问题:这究竟是什么意思? [英] jQuery question: what does it really mean?
问题描述
(function($, window, undefined){
... jquery code...
})(jQuery, window);
这究竟是什么意思?它是否也意味着 $(文件).ready()
?或者只是两个不同的东西?
What does it really mean? Does it also mean $(document).ready()
? Or just two different things?
推荐答案
已经有两个答案,但这是我对代码缺失结束的猜测:
There are already two answers but this is my guess on the missing end of the code:
(function ($, window, undefined) {
// ... jquery code...
})(jQuery, window);
注意:三个参数是预期但两个是提供的。
Note: three parameters are expected but two are supplied.
它基本上是做什么的:
- 给出一个私人的花括号里面的范围,所以在里面声明的任何
var
在 之外是不可见的。 - 使私有
$
jQuery
的快捷方式,而不依赖于全局设置的此快捷方式(例如。jQuery.noconflict()
可能已被调用,这仍然可以工作) - 创建一个词法窗口变量,这意味着更快地查找全局变量
- 确保
undefined
在大括号之间的范围内确实是未定义的,即使有人写了类似undefined =now now the defined;
在全局范围内,因为undefined
实际上可以重新定义(这是该语言的错误)。
- gives a private scope inside curly braces, so any
var
declared inside is not visible outside - makes a private
$
shortcut tojQuery
without relying on this shortcut being set globally (eg.jQuery.noconflict()
might have been called and this would still work) - makes a lexical window variable that would mean faster lookups for global variables
- makes sure that
undefined
is indeed undefined in the scope between curly braces, even if someone has written something likeundefined = "now it's defined";
in the global scope, becauseundefined
can actually be redefined (this is a mistake of the language).
此模式立即称为调用函数,或简短的立即函数,或自调用匿名函数或其他一些名称。基本思路是:
This pattern is known as immediately invoked function, or immediate function for short, or self-invoking anonymous function, or some other names. The basic idea is that:
(function (x, y) {
// ...
})(1, 2);
或:
(function (x, y) {
// ...
}(1, 2));
表示相同:
function myFunction (x, y) {
// ...
}
myFunction(1, 2);
但不需要为您的函数指定任何名称并污染命名空间。
but without the need to give any name to your function and pollute the namespace.
回到你的问题,这并不意味着 $(文件).ready()
或类似的东西,但这意味着您可以使用 $(document).ready()
inside 而不是 jQuery(document).ready()
即使 $
快捷方式在外面不可用。
Going back to your question, this doesn't mean $(document).ready()
or anything like that, but it means that you can use $(document).ready()
inside instead of jQuery(document).ready()
even if the $
shortcut is not available outside.
这个例子实际上可以解释它更好,即使它没有在任何地方使用:
This example may actually explain it better, even if it isn't used anywhere:
(function (JQ, window, undefined) {
JQ(document).ready(function () {
// this is run when document is ready
});
})(jQuery, window);
现在可以调用jQuery而不是 $
as JQ
并使用 JQ(文件).ready()
而不是 $(文件) .ready()
- 它可能看起来不太有用,但它显示了当你有这样的代码时会发生什么。
Now instead of $
you can call jQuery as JQ
and use JQ(document).ready()
instead of $(document).ready()
– it may not seem very useful but it shows what happens when you have a code like that.
作为旁注我可以补充说,多亏了这个模式,你实际上并不需要语言中的变量声明,只需要函数参数。而不是:
As a side note I might add that thanks to this pattern you don't actually need variable declarations in the language but only function arguments. Instead of:
var x = 10;
alert(x * x * x);
你可以使用:
(function (x) {
alert(x * x * x);
})(10);
确实是这样的函数:
function square (x) {
// some code ...
var result = x * x;
return result;
}
完全等同于:
function square (x, result) {
// some code ...
result = x * x;
return result;
}
因为JavaScript中的提升机制会使结果
变量可用(但未定义)甚至在两种情况下的声明和赋值之前(在 //某些代码...
部分)。这通常是一个混乱的来源,但实际上非常有趣且非常强大。
because of the hoisting mechanism in JavaScript that would make the result
variable available (but undefined) even before the declaration and assignment in both cases (in the // some code ...
part). This is often a source of confusion but is actually quite interesting and extremely powerful.
另请参阅我最近更新的其他回答问题:
帮助理解JavaScript全局减排技术,了解有关此主题的更多信息。
See also my other recently updated answer to the question: Help understanding JavaScript global abatement techniques for more info on this subject.
这篇关于jQuery问题:这究竟是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!