为什么是“$().ready(handler)"?不推荐? [英] Why "$().ready(handler)" is not recommended?
问题描述
来自 jQuery API 文档站点,用于 ready
以下三种语法都是等价的:
All three of the following syntaxes are equivalent:
- $(document).ready(handler)
- $().ready(handler)(不推荐)
- $(handler)
做完功课 - 阅读和玩源代码,我不知道为什么>
After doing homework - reading and playing with the source code, I have no idea why
$().ready(handler)
不推荐.第一种和第三种方式,完全一样,第三种方式使用document
在缓存的jQuery对象上调用ready函数:
is not recommended. The first and third ways, are exactly the same, the third option calls the ready function on a cached jQuery object with document
:
rootjQuery = jQuery(document);
...
...
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
但是ready函数和被选中节点元素的selector没有交互,ready
源码:
But the ready function has no interaction with the selector of the selected node elements, The ready
source code:
ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
// Add the callback
readyList.add( fn );
return this;
},
如您所见,它只是将回调添加到内部队列(readyList
),并且不会更改或使用集合中的元素.这让您可以在每个 jQuery 对象上调用 ready
函数.
As you can see, it justs add the callback to an internal queue( readyList
) and doesn't change or use the elements in the set. This lets you call the ready
function on every jQuery object.
喜欢:
- 常规选择器:
$('a').ready(handler)
演示 - 废话选择器:
$('fdhjhjkdafdsjkjriohfjdnfj').ready(handler)
演示 - 未定义选择器:
$().ready(handler)
DEMO一个>
- regular selector:
$('a').ready(handler)
DEMO - Nonsense selector:
$('fdhjhjkdafdsjkjriohfjdnfj').ready(handler)
DEMO - Undefined selector:
$().ready(handler)
DEMO
最后...我的问题:为什么不推荐使用 $().ready(handler)
?
Finally... to my question: Why $().ready(handler)
is not recommended?
推荐答案
我从一位 jQuery 开发人员那里得到了官方答复:
$().ready(fn)
之所以有效,是因为 $()
曾经是 $(document)
(jQuery <1.4)
所以 $().ready(fn)
是一个可读的代码.
$().ready(fn)
only works because $()
used to be a shortcut to $(document)
(jQuery <1.4)
So $().ready(fn)
was a readable code.
但是人们过去常常做诸如 $().mouseover()
之类的事情以及其他各种疯狂的事情.
人们不得不做 $([])
来获得一个空的 jQuery 对象
But people used to do things like $().mouseover()
and all sorts of other madness.
and people had to do $([])
to get an empty jQuery object
所以在 1.4 中我们改变了它,所以 $()
给出一个空的 jQuery,我们只是让 $().ready(fn)
工作,以免破坏大量代码
So in 1.4 we changed it so $()
gives an empty jQuery and we just made $().ready(fn)
work so as not to break a lot of code
$().ready(fn)
现在实际上只是在内核中打了补丁,使其在遗留案例中正常工作.
$().ready(fn)
is literally now just patched in core to make it work properly for the legacy case.
ready
函数的最佳位置是 $.ready(fn)
,但这是一个非常古老的设计决策,这就是我们现在所拥有的.
The best place for the ready
function is $.ready(fn)
, but it's a really old design decision and that is what we have now.
我问他:
你认为 $(fn) 比 $().ready(fn) 更易读吗?!
Do you think that $(fn) is more readable than $().ready(fn) ?!
他的回答是:
我总是在实际的应用程序中执行 $(document).ready(fn) 并且通常应用程序中只有一个文档就绪块,这与维护工作并不完全一样.
I always do $(document).ready(fn) in actual apps and typically there's only one doc ready block in the app it's not exactly like a maintenance thing.
我认为 $(fn) 也很不可读,它只是你必须知道的事情™...
这篇关于为什么是“$().ready(handler)"?不推荐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!