extend jQuery in () method There are three main uses:
// extension method invocation: $ A (). $ .Extend ({ A: function { Alert ( . 1 ) } }) // merge the first object into a $ .extend (a, {name: 'nick'}, Age {: '30 ' }) // copy var A = {}; var B = {Nage:' Nick ' } $ .extend (A, B) // shallow copy $ .extend ( to true , A, B) / / shallow copy
Source portion thereof as follows:
= = jQuery.fn.extend jQuery.extend function () { var Options, name, the src, Copy, copyIsArray, clone, target = arguments [0] || {}, // the argument list that is a target value i . 1 = , length = the arguments.length, deep = to false ; // shallow copy when deep copy and shallow copy default iF ( typeof target === "Boolean" ) {// determines whether the copy target replacement deep = target; target arguments = [. 1] || {}; I = 2 ; } IF (typeof ! target == "Object" &&! jQuery.isFunction (target)) {// check whether it's correct target = {}; } IF (length === I) {// only one object parameter indicating widget call target = the this ; - I; } for (; I <length; I ++ ) { IF (! (Options = arguments [I]) = null ) {// meaningful determination for (name in Options) { the src = target [name ]; Copy = Options [name]; IF(=== target copy) { Continue ; // prevented if the value of the target and a circular reference copy are equal, it will result in consistent cycle continues } IF (Deep copy && && (jQuery.isPlainObject (copy) || (= copyIsArray the jQuery. isArray (copy)))) { // deep deep copy is true IF (copyIsArray) {// recursive end copyIsArray = to false ; clone = jQuery.isArray the src && (the src)? the src: []; } the else { clone = the src ? && jQuery.isPlainObject (the src) the src: {}; } target [name] =jQuery.extend (deep, clone, copy) ; // recursive call } the else IF (! Copy == undefined) {// shallow copy target [name] = Copy; } } } } return target; };
First, jQuery.extend = jQuery.fn.extend = function () {...}, jQuery is a function to add a function to the following method, called static expansion method, is a reference to the jQuery.fn jQuery.prototyped of i.e. jQuery prototype, the method of adding to the prototype method is called the extended example below. Such direct $ .fn when you call the extended static method () on it, and call an instance method requires $ (). Fn (). Subsequently source and defines a number of variables, code comments already stated the meaning of variables. Then started is determined whether the target value is a Boolean value, if the Boolean value would indicate the need to copy, the target value for the second parameter change.
As for the deep and shallow copy copy in learning tomorrow.