Adding callbacks and triggered events to a plugin
I've added both callbacks and triggered events to a plugin as follows:
// located at the point where the event and callback should be called
el.trigger('initialized', el);
if ($.isFunction(options.onInitialize)) { options.onInitialize(el); }
but I found anothe开发者_开发百科r method where it was done this way:
// 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);
So, my question is does it matter that the event is triggered before the callback in the first method or should I switch to using the second method where they both occur at the same time?
Update: The only reason I've come up with so far is minimizing function calls - $.isFunction
is only called once in the second example.
I ended up going with the second method. Since the $.isFunction()
is called each cycle it just seems to be more efficient to only do it once. I could have cached the result as well... so I guess there is either method will work just as well.
精彩评论