内联函数调用有什么好处? [英] What's the benefit of inline function calls?
问题描述
我看到了这段代码(显然是在jQuery中,经过修改)
I saw this code (explicly it's in jQuery, with modification)
(function(window,undefined){
var jQuery=(function(){
var jQuery=something;
jQuery.xxx=xxx;
//...
return jQuery;
})();
//...
window.jQuery=window.$=jQuery;
})(window);
虽然我理解在内联函数调用中包装代码可以清楚地定义变量范围,但我不知道了解
While I understand wrapping code in a inline function call can clearly define the variable scope, I don't understand the benefits of
- 使用参数传递
窗口
的好处,而不是直接使用它, - 通过未定义的参数获取
undefined
的实例,以及 - 定义
jQuery
由另一个内联函数调用的返回值。
有人可以解释一下吗?
- passing
window
with a parameter instead of using it directly, - getting an instance of
undefined
by a undefined parameter, and also - defining
jQuery
by the return value of another inline function call. Can somebody explain a bit?
EDIT 写#3更清楚:
我的理解是代码在另一个函数中定义 jQuery
然后返回它。
What I understand is that the code defines jQuery
inside another function then return it.
//(function(window,undefined){
var jQuery=(function(){
// Inside this function defines jQuery and return it?
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
return jQuery;
})(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);
这更像是以下内容:
function define_jQuery(){
// Inside this function defines jQuery and return it?
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
return jQuery;
}
//(function(window,undefined){
var jQuery=define_jQuery(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);
这样做不会更简单:
//(function(window,undefined){
var jQuery=function(selector,context){
return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
//... })(window);
推荐答案
分别回答这些问题:
-
为什么
窗口
传入?因为在JavaScript中解除引用变量是很痛苦的。传递实例意味着您没有必要。通常机制如下所示:
Why
window
passed in? Because dereferencing a variable in JavaScript is painful. Passing in an instance means you don't have to. Typically the mechanism looks like this:
(function (window, document, $) {
}(window, window.document, jQuery));
在这种情况下,不需要去全局范围取消引用这三种中的任何一种(和jQuery)可以在 .noConflict()
中启动。)
In this scenario, one need not go to the global scope to dereference any of these three (and jQuery can be in .noConflict()
to boot).
这是有效的JavaScript: undefined = 2;
。我承认这是非常愚蠢的,但这是可能的。但是如果一个函数接受一个函数而不是传递一个参数,那么人们相信它确实是 undefined
而不是它的黑客副本。
This is valid JavaScript: undefined = 2;
. I grant that is very foolish, but it is possible. But if one accepts one more argument in a function than is passed, one is confident it is truely undefined
and not a hacked copy of it.
从前一个函数返回jQuery允许方法链接: $('#sel')。func1()。func2()
。这是可能的,因为func1可能看起来像这样:
Returning jQuery from a previous function allows method chaining: $('#sel').func1().func2()
. This is possible because func1 probably looks something like this:
jQuery.fn.func1 = function () {
return $(this).each(function () {
// do something fabulous
};
};
返回$(this).bla_de_bla()
是:
$(this).bla_de_bla(..);
return $(this);
它还假设.bla_de_bla()也返回 $(this)
It also presumes that .bla_de_bla() also returns $(this)
编辑:修改#3,注意最好是链接而不是环绕 .noConflict()
并错误命名 $
。
modified #3 to note that it's best at chaining rather than circumnavigating around .noConflict()
and mis-named $
.
这篇关于内联函数调用有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!