请解释 jQuery-ui 项目中的这些行 - 与添加“子插件”有关。
jquery.effects.core 的第 10 - 16 行。 js:
;jQuery.effects || (function($, undefined) {
var backCompat = $.uiBackCompat !== false; // Irrelevant
$.effects = {
effect: {}
};
})(jQuery); // At end of file
据我了解,这增加了一个effects
“命名空间”,但前提是它尚不存在。
有人可以向我解释一下:
- 第一个分号的作用是什么?
undefined
参数的用途是什么?undefined
的含义是否以某种方式被覆盖?- 直接向
jQuery
对象添加函数与按照 jQuery 文档? - 最后,如果我想创建一堆仅供我自己的团队使用的 jQuery 插件,那么使用上面的代码将它们全部集中在公司命名空间下是否有意义?
编辑:我现在意识到 jQuery.effects 可能是一个坏例子。我看到 jQuery.ui.core.js 的做法有所不同:
(function( $, undefined ) {
$.ui = $.ui || {};
// add some stuff to $.ui here
$.fn.extend({
// plugins go here
});
})(jQuery);
但是如果添加了插件,ui
对象有什么用直接到 $.fn
吗?我可以在 $.fn
下定义我的命名空间并将所有插件添加到 $.fn.acme
中,以便我像这样使用它们: $('something ').acme.doStuff()
?
对于这类事情有最佳实践吗?
Lines 10 - 16 of jquery.effects.core.js:
;jQuery.effects || (function($, undefined) {
var backCompat = $.uiBackCompat !== false; // Irrelevant
$.effects = {
effect: {}
};
})(jQuery); // At end of file
As I understand it, this adds an effects
"namespace", but only if it doesn't already exist.
Can someone explain to me:
- What is the initial semi-colon for?
- What is the purpose of the
undefined
parameter? Is the meaning ofundefined
overridden in some way? - What's the difference between adding a function directly to the
jQuery
object, and adding one tojQuery.fn
as recommended in the jQuery documentation? - Finally, if I wanted to create a bunch of jQuery plugins that would only be used by my own team, would it make sense to lump them all under a company namespace using something like the code above?
Edit: I realize now jQuery.effects is probably a bad example. I see jQuery.ui.core.js does it differently:
(function( $, undefined ) {
$.ui = $.ui || {};
// add some stuff to $.ui here
$.fn.extend({
// plugins go here
});
})(jQuery);
But what is the use of the ui
object if plugins are added directly to $.fn
anyway? Could I define my namespace under $.fn
and add all my plugins to $.fn.acme
, so that I use them like so: $('something').acme.doStuff()
?
Is there a best practice for this sort of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(function() { ... } (jquery)
,由于与范围和冲突等相关的原因,它传递 jQuery 对象。所以,对于您的问题:
1 。
我认为没有什么特别的。干净的声明有一些边缘情况。在此之前的最后一行是函数声明 close 。
2 。未定义的参数的含义是什么?
它只是确保以后不会直接传递全局对象。 3. 直接向 jQuery对象
添加函数与按照 jQuery 文档中的建议向 jQuery.fn 添加函数有什么区别?
这是 jQuery 的结构方式和一般组织问题。函数并返回一个对象。
.fn
负责注册此对象以应用于返回的 jQuery 对象(来自 jQuery select 等),因此,这样更好,以便 jQuery 实际上了解您添加的功能。4.最后,如果我想创建一堆仅供我自己的团队使用的 jQuery 插件,那么使用上面的代码将它们全部集中在公司命名空间下是否有意义?
大多数人不这样做。不推荐它。也许一个常见的“小”前缀就足够了。
jQuery.effects
exists(function() { ... } (jquery)
, it passes jQuery object for reasons related to scope and conflict and such.So, to your questions:
1 . What is the initial semi-colon for?
I think nothing special. Just ensuring clean statement. This has some edge cases if the last line before this one was a function declaration close.
2 . What is the purpose of the undefined parameter? Is the meaning of undefined overridden in some way?
It just ensures this doesn't happen later. Passes the global object directly. Common pattern I think.
3 . What's the difference between adding a function directly to the jQuery object, and adding one to jQuery.fn as recommended in the jQuery documentation?
It's the way jQuery is structured and general organization issue. The jQuery object is a function and returns an object. The
.fn
handles registering this one to apply on returned jQuery objects (from jQuery select or so), so, that's better so that jQuery actually knows about your added function.4 . Finally, if I wanted to create a bunch of jQuery plugins that would only be used by my own team, would it make sense to lump them all under a company namespace using something like the code above?
Most people don't do it. Wouldn't recommend it. Maybe a common "small" prefix is enough.