为什么“$()。ready(handler)不推荐? [英] Why "$().ready(handler)" is not recommended?
问题描述
从 jQuery API文档网站为准备
以下所有三种语法都是等效的:
- $(document).ready(handler)
- $()。ready(处理程序) li> $(handler)
完成家庭作业后, http://code.jquery.com/jquery-1.7.2.js\">源代码,我不知道为什么
$()。ready(handler)
第一种和第三种方法是完全相同的,第三个选项调用缓存的jQuery对象上的ready函数 document
:
rootjQuery = jQuery(document);
...
...
// HANDLE:$(function)
//文档快捷方式
} else if(jQuery。 isFunction(selector)){
return rootjQuery.ready(selector);
}
但是ready函数没有与所选节点元素的选择器交互, ready
源代码:
ready:function(fn){
//附加监听器
jQuery.bindReady();
//添加回调
readyList.add(fn);
return this;
},
正如你所看到的,它只是将回调添加到内部队列 readyList
),并且不更改或使用集合中的元素。这可以让您在每个jQuery对象上调用 ready
函数。
Like:
- 正规选择器:
$('a')。ready(handler)
DEMO - 无意义选择器:
$ ('fdhjhjkdafdsjkjriohfjdnfj')。ready(handler)
DEMO - 未定义选择器:
$()。ready(handler)
DEMO
最后...我的问题:为什么 $ ) 从jQuery开发人员的官方回答:
$()。ready(fn)
只有因为 $()
用于 $(document)
(jQuery< 1.4)
因此 $()。ready(fn)
是可读代码。
但是人们以前做过 $()。mouseover()
和其他各种疯狂。
, $ $([])
获得一个空的jQuery对象
$ c> $()给出一个空的jQuery,我们只是使 $()。ready(fn)
的代码
$()。ready(fn)
现在只是修补在核心,
ready
函数的最佳位置是 $。 (fn)
,但这是一个非常旧的设计决定,这是我们现在的。
我问他:
你认为$(fn)比$()更容易读吗?
他的回答是:
总是在实际的应用程序中执行$(document).ready(fn),通常在应用程序中只有一个文档准备好的块,它不是一个维护的东西。
我认为$(fn)也是不可读的,它只是你必须知道的事物™ ...
From the jQuery API docs site for ready
All three of the following syntaxes are equivalent:
- $(document).ready(handler)
- $().ready(handler) (this is not recommended)
- $(handler)
After doing homework - reading and playing with the source code, I have no idea why
$().ready(handler)
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 );
}
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;
},
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.
Like:
- regular selector:
$('a').ready(handler)
DEMO - Nonsense selector:
$('fdhjhjkdafdsjkjriohfjdnfj').ready(handler)
DEMO - Undefined selector:
$().ready(handler)
DEMO
Finally... to my question: Why $().ready(handler)
is not recommended?
I got an official answer from one of the jQuery developers:
$().ready(fn)
only works because $()
used to be a shortcut to $(document)
(jQuery <1.4)
So $().ready(fn)
was a readable code.
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
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)
is literally now just patched in core to make it work properly for the legacy case.
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.
I asked him:
Do you think that $(fn) is more readable than $().ready(fn) ?!
His answer was:
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.
I think $(fn) is pretty unreadable too, it's just A Thing That You Have To Know Works™...
这篇关于为什么“$()。ready(handler)不推荐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!