请解释 jQuery-ui 项目中的这些行 - 与添加“子插件”有关。

发布于 2024-12-14 12:44:30 字数 1439 浏览 2 评论 0原文

jquery.effects.core 的第 10 - 16 行。 js

;jQuery.effects || (function($, undefined) {

    var backCompat = $.uiBackCompat !== false; // Irrelevant

    $.effects = {
        effect: {}
    };

})(jQuery); // At end of file

据我了解,这增加了一个effects“命名空间”,但前提是它尚不存在。

有人可以向我解释一下:

  1. 第一个分号的作用是什么?
  2. undefined 参数的用途是什么? undefined 的含义是否以某种方式被覆盖?
  3. 直接向 jQuery 对象添加函数与按照 jQuery 文档
  4. 最后,如果我想创建一堆仅供我自己的团队使用的 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:

  1. What is the initial semi-colon for?
  2. What is the purpose of the undefined parameter? Is the meaning of undefined overridden in some way?
  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?
  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?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-12-21 12:44:30
  • 它检查 jQuery.effects 是否存在,
  • 如果不存在,它定义一个函数并同时调用
    (function() { ... } (jquery),由于与范围和冲突等相关的原因,它传递 jQuery 对象。
  • 该函数中的第一行据说是不相关的,似乎是检查 jQuery 插件属性的存在
  • 它为 jQuery 插件属性定义了一个占位符(如命名空间或容器类)。

所以,对于您的问题:

1 。

我认为没有什么特别的。干净的声明有一些边缘情况。在此之前的最后一行是函数声明 close 。

2 。未定义的参数的含义是什么?

它只是确保以后不会直接传递全局对象。 3. 直接向 jQuery对象

添加函数与按照 jQuery 文档中的建议向 jQuery.fn 添加函数有什么区别?

这是 jQuery 的结构方式和一般组织问题。函数并返回一个对象。 .fn 负责注册此对象以应用于返回的 jQuery 对象(来自 jQuery select 等),因此,这样更好,以便 jQuery 实际上了解您添加的功能。

4.最后,如果我想创建一堆仅供我自己的团队使用的 jQuery 插件,那么使用上面的代码将它们全部集中在公司命名空间下是否有意义?

大多数人不这样做。不推荐它。也许一个常见的“小”前缀就足够了。

  • It checks if jQuery.effects exists
  • If not, it defines a function and calls in the same time
    (function() { ... } (jquery), it passes jQuery object for reasons related to scope and conflict and such.
  • The first line in that function is said to be irrelevant, it seems to be checking a presence of a jQuery plugin property
  • It defines a placeholder (like namespace or container class) for the effects jQuery plugin property.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文