jquery 小部件、_create 或 _init

发布于 2024-09-28 12:53:23 字数 111 浏览 3 评论 0原文

有些jquery插件扩展小部件使用_create方法,而另一些则使用_init方法,有人可以解释两者之间的区别吗?

还有什么时候扩展 widget 或直接扩展 jquery.fn 更好的指导?

Some jquery plugin extend widget use _create method, while others use _init method, can someone explain the differences between the two?

Also any guidance on when it is better to extend widget or directly extend jquery.fn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

也只是曾经 2024-10-05 12:53:23

来自 jQuery UI 开发人员指南

创建元素后,对第一个参数不是字符串的小部件名称的所有其他调用都将调用 _init() 方法;如果传递选项,.option() 方法将在 _init() 方法之前调用

From the jQuery UI Developer Guide:

Once the element is created, all other calls to the widget name where the first parameter is not a string will call the _init() method; if options are passed, the .option() method will be called before the _init() method

养猫人 2024-10-05 12:53:23

扩展小部件(与 $.fn 相对)的缺点是您创建了对定义小部件“类”的 jquery-ui 的依赖关系。对于不使用 jquery-ui 的插件用户来说,这种依赖关系可能会很昂贵。

就 _create 与 _init 而言,我非常确定 _init 排在第一位,然后在最近的修订版中引入并支持 _create。我对此可能是错的,但我相信 _init 仍然受到支持。如果是的话,那么两者之间应该没有任何区别。

The downside to extending widget (as opposed to $.fn) is that you create a dependency on jquery-ui which defines the widget "class". That dependency could be expensive for users of your plugin that don't also use jquery-ui.

As far as _create vs _init goes, I'm pretty sure that _init came first and then in a recent revision they introduced and favor _create. I might be wrong about this, but I believe that _init is still supported. If it is then there shouldn't be any differences between the two.

_蜘蛛 2024-10-05 12:53:23

小部件具有三个阶段:

+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
| Phase | Name           | jQuery Method | Description                                         |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
|     1 | Creation       | _create       | First time the widget is applied to an element, it is called.                              |  
|     2 | Initialization | _int          | The _init method is called after _create when the widget is first applied to its elements. |  
|     3 | Destruction    | destroy       | The widget's destroy method is used to detach a widget from an element.                    |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+

注意:以下划线开头的方法名称按照惯例意味着是私有的。


所以_create_init之间是有区别的。一个用于创建,另一个用于初始化。每次您不带参数或带选项调用小部件时,它都会间接调用 _init 方法。因此,这可用于重置(重新初始化)小部件或向其传递不同的选项。

有关每个阶段的更多详细信息,请参见此处

A widget has three phases:

+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
| Phase | Name           | jQuery Method | Description                                         |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+
|     1 | Creation       | _create       | First time the widget is applied to an element, it is called.                              |  
|     2 | Initialization | _int          | The _init method is called after _create when the widget is first applied to its elements. |  
|     3 | Destruction    | destroy       | The widget's destroy method is used to detach a widget from an element.                    |  
+-------+----------------+---------------+--------------------------------------------------------------------------------------------+

NOTE: The method names starting with an underscore are meant to be private by convention.


So there is a difference between _create and _init. One is used for creation and the other is used for initialization. Every time you call the widget with no arguments or with options, it will indirectly call _init method. Therefore, this can be used to reset (re-initialize) a widget or pass it different options.

More details about each phase here.

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