我正在使用 RequreJS,需要在 DOM 上初始化一些东西。现在,RequreJS 提供了 domReady
插件,但是我们已经有了 jQuery 的 $(document).ready()
,它对我来说是可用的,因为我需要 jQuery。
所以我有两个选择:
使用 domReady
插件:
require(['domReady'], function (domReady) {
domReady(function () {
// Do my stuff here...
});
});
Use $(document).ready()
:
$(document).ready(function() {
// Do my stuff here...
});
Which one should I choose, and why?
Both the options seems to work as expected. I am not confident in the jQuery's one because RequireJS is doing its magic; that is, since RequireJS will dynamically add scripts, I'm worried that DOM ready may occur before all of the dynamically-requested scripts are loaded. Whereas, RequireJS will add a burden on additional JS just for domReady
when I already have jQuery required.
domReady
plugin when we can have jQuery's $(document).ready();
? I don't see any advantage of including another dependency.As far as I know, a module that requires domReady
won't be fetched or executed after the document is ready, and you could do the same requiring jQuery as well:
require(['jQuery'], function ($) {
$(document).ready(function () {
// Do my stuff here...
});
});
为了更清楚地回答我的问题: 要求 ABC0和 jQuery
有什么区别?