Target:
$.extend ({
Add:function (A, b) {
return a + B;
}
})
Console.log ($.add);How is it possible to hang the add function behind jquery?jquery-v1.12.4
1Jquery.extend = JQuery.fn.extend =function() {2 varsrc, copyisarray, copy, name, options, clone,3target = arguments[0] | | {},4i = 1,5Length =Arguments.length,6Deep =false;7 8 //Handle a deep copy situation9 if(typeoftarget = = = "Boolean" ) {TenDeep =Target; One A //Skip the Boolean and the target -target = arguments[I] | | {}; -i++; the } - - //Handle Case If Target is a string or something (possible in deep copy) - if(typeofTarget!== "Object" &&!jquery.isfunction (target)) { +target = {}; - } + A //extend JQuery itself if only one argument is passed at if(i = = =length) { -target = This; -i--; - } - - for(; i < length; i++ ) { in - //Only deal with non-null/undefined values to if(options = arguments[i])! =NULL ) { + - //Extend the Base object the for(Nameinchoptions) { *src =target[name]; $copy =options[name];Panax Notoginseng - //Prevent Never-Ending Loop the if(target = = =copy) { + Continue; A } the + //Recurse If we ' re merging plain objects or arrays - if(Deep && copy && jquery.isplainobject (copy) | | $(Copyisarray =jquery.isarray (copy))) ) { $ - if(Copyisarray) { -Copyisarray =false; theclone = src && jquery.isarray (src)?src: []; - Wuyi}Else { theclone = src && jquery.isplainobject (src)?src: {}; - } Wu - //never move original objects, clone them Abouttarget[name] =jquery.extend (deep, clone, copy); $ - //Don ' t bring in undefined values -}Else if(Copy!==undefined) { -target[name] =copy; A } + } the } - } $ the //Return the Modified object the returnTarget; the};
Analysis:Line 3rd: Take the function add第23-26: Assigns this, the instance object of jquery, function (selector, context) {} to target.Line 31st: Assign the function add to the options and iterate. line 36th: options = {Add:function (A, b) {...}} Assign a value to copy.line 60th: by Target[name]=copy; Adding a property to the jquery instance adds the jquery[' Add ']=function (A, b) {return a+b};
$.extend () Realization of source code-(source learning 1)