I was looking for a suitable way to extend the bootstrap plugin and found this answer: https://stackoverflow.com/a/165778/
What bothers me is the last section - overriding initialization. The code copied 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 - is there an 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 is equal to f2. So it seems that simple assimilation would do,
$.fn.modal = function(option) {...}
but maybe I'm missing something ...
jquery extend twitter-bootstrap
Arek Dygas Sep 28 '16 at 15:03 2016-09-28 15:03
source share