题
我在插件中添加了回调和触发事件,如下所示:
// 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
在第二个示例中仅调用一次。
解决方案
我最终使用了第二种方法。自从 $.isFunction()
被称为每个周期,似乎只能进行一次更有效。我也可以缓存结果...所以我想有任何方法也可以正常工作。
不隶属于 StackOverflow