jquery 小部件、_create 或 _init
有些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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 jQuery UI 开发人员指南:
From the jQuery UI Developer Guide:
扩展小部件(与 $.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.
小部件具有三个阶段:
注意:以下划线开头的方法名称按照惯例意味着是私有的。
所以
_create
和_init
之间是有区别的。一个用于创建,另一个用于初始化。每次您不带参数或带选项调用小部件时,它都会间接调用_init
方法。因此,这可用于重置(重新初始化)小部件或向其传递不同的选项。有关每个阶段的更多详细信息,请参见此处。
A widget has three phases:
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.