I was searching for a proper way of extending bootstrap plugin, and found this answer: https://stackoverflow.com/a/12689534/1276032
What troubles me, is the last section - initialization overriding. Copied code below:
// override the old initialization with the new constructor
$.fn.modal = $.extend(function(option) {
var args = $.makeArray(arguments),
option = args.shift();
return this.each(function() {
var $this = $(this);
var data = $this.data('modal'),
options = $.extend({}, _super.defaults, $this.data(), typeof option == 'object' && option);
if ( !data ) {
$this.data('modal', (data = new Modal(this, options)));
}
if (typeof option == 'string') {
data[option].apply( data, args );
}
else if ( options.show ) {
data.show.apply( data, args );
}
});
}, $.fn.modal);
I don't understand why $.extend is used in this case - does it have some effect that I don't see? If I execute this code:
var f1 = function(){console.log(1);};
var f2 = function(){console.log(2);};
var f2 = $.extend(f1,f2);
f2();
then only 1 is printed to the console, and f1 equals f2. So it seems simple assingnment would do,
$.fn.modal = function(option) {...}
but maybe I miss something...