我已将回调和触发事件添加到插件中,如下所示:
// located at the point where the event and callback should be called
el.trigger('initialized', el);
if ($.isFunction(options.onInitialize)) { options.onInitialize(el); }
但我找到了另一种以这种方式完成的方法:
// located at the beginning of the script, after the options are extended
if ($.isFunction(options.onInitialize)) { el.bind('initialized', options.onInitialize; }
// located at the point where the event should be called
el.trigger('initialized', el);
所以,我的问题是在第一种方法的回调之前触发事件是否重要,或者我应该切换到使用它们同时发生的第二种方法?
更新:到目前为止我想出的唯一原因是最小化函数调用 -$.isFunction
在第二个示例中只调用一次。