2010-11-11 26 views
3

我已经添加了两种回调和触发的事件,以插件如下:添加回调和触发的事件,以插件

// 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只在第二个示例中调用一次。

回答

0

我结束了第二种方法。由于每个周期都调用$.isFunction(),因此只做一次似乎更有效率。我可以缓存结果以及...所以我想这两种方法都可以起作用。