I am trying to specify options for an objects method and one of the options is a callback function. I am using the jQuery extend
method to apply my default values to the options parameter but am unsure what value I should set for the callback parameter.
This is what I have at the moment:
var options = $.extend({
wrapper: '#wrapper', // Define the default wrapper to prepend to
fade: true, // Fade the overlay by default
callback: function(){} // Define the callback function
},options);
Is the above the best method for assigning a default value to the callback? Or would it be better to set it to false? I thought if you tried to call options.callback()
and the callback is set to false then you would receive an error?
Having a non function default requires the explicit check before invocation.
Having an empty function requires the overhead of invoking it (though minimal).
There's no right way. It's totally up to you. If you do use an empty function, you can use $.noop
to reuse an existing one.
var options = $.extend({
wrapper: '#wrapper', // Define the default wrapper to prepend to
fade: true, // Fade the overlay by default
callback: $.noop // Define the callback function
},options);