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...
You need to change this:
$.fn.modal = $.extend(function(option) {
// your code ...
}, $.fn.modal);
for this:
$.extend($.fn.modal, function(option) {
// your code ...
});
TL;DR
$.extend(a, b)
copies the content of b on a (modifying it's content), and if any repeated, then the property of b remains. Also, it returns the value of a.
So, If you have this:
hello = { unique_on_hello: 'hola', message: 'hello message' }
world = { unique_on_world: 'mundo', message: 'WORLD MESSAGE' }
response = $.extend(hello, world)
The values of each one will be:
hello // {unique_on_hello: "hola", message: "WORLD MESSAGE", unique_on_world: "mundo"}
world // {unique_on_world: "mundo", message: "WORLD MESSAGE"}
response // {unique_on_hello: "hola", message: "WORLD MESSAGE", unique_on_world: "mundo"}
So, if you do f2 = $.extend(f1,f2);
is the same as:
$.extend(f1, f2) // Copy properties of f2 to f1
f2 = f1
来源:https://stackoverflow.com/questions/39751235/what-might-be-a-reason-of-overriding-function-using-jquery-extend