jquery extends

16061 단어
 、jQuery extend    

jQuery API   ,extend     jQuery jQuery.fn         ,  jQuery           ,         ;

        :

jQuery.extend(): Merge the contents of two or more objects together into the first object.(                  );

jQuery.fn.extend():Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(      jQuery prototype  ,       jQuery    )         : jQuery.extend(object);    jQuery   ,         。 jQuery.fn.extend(object); jQuery      。  、jQuery extend     1、jQuery.extend(object); (a) jQuery.extend( target [, object1 ] [, objectN ] )   object1, objectN target  ,        ,  target       jQuery   ,    : 1. var object1 = { 2. apple: 0, 3. banana: { weight: 52, price: 100 }, 4. cherry: 97 5. }; 6. var object2 = { 7. banana: { price: 200 }, 8. durian: 100 9. }; 10. 11.// Merge object2 into object1 12.$.extend( object1, object2 ); 13.console.log(object1.durian); //100 14. 15.// Merge object1 into jQuery 16.$.extend( object1 ); 17.console.log( $.apple ); //0  (2) jQuery.extend( [deep ], target, object1 [, objectN ] )         ,      boolean   true , object1, objectN        target ;      ,   null, undefined,window  ,dom  ,                     target ;       ,      ,       (  dom window  ,      ,    ,          ,        ,          );            ,                (   、  、json   ),           ,                 ,     : 1. obj1 = { a : 'a', b : 'b' }; 2. obj2 = { x : { xxx : 'xxx', yyy : 'yyy' }, y : 'y' }; 3. $.extend(true, obj1, obj2); 4. alert(obj1.x.xxx); //   "xxx" 5. obj2.x.xxx = 'zzz'; //  obj2        ,        obj1 6. alert(obj2.x.xxx); //   "zzz" 7. alert(obj1.x.xxx); //   "xxx" //   ;    true,   “zzz”         ,         …… 2、jQuery.fn.extend(object); jQuery.fn = jQuery.prototype    jQuery      ,        ,   jQuery    ;         jQuery      1. // hello     jquery      。 2. $.fn.extend({ 3. hello:function(){alert('hello');} 4. }); 5. 6. // jquery         net    。 7. $.extend($.net,{ 8. hello:function(){alert('hello');} 9. }); //  jQuery.net.hello();  、jQuery extend      extend()   jQuery       ,          。       : 1. <script type="text/javascript" src="jquery-1.5.2.js"></script> 2. <script> 3. obj1 = { a : 'a', b : 'b' }; 4. obj2 = { x : { xxx : 'xxx', yyy : 'yyy' }, y : 'y' }; 5. 6. $.extend(true, obj1, obj2); 7. 8. alert(obj1.x.xxx); //   "xxx" 9. 10.obj2.x.xxx = 'zzz'; 11.alert(obj2.x.xxx); //   "zzz" 12.alert(obj1.x.xxx); //   "xxx" 13.</script>   $.extend(true, obj1, obj2)   obj2        obj1,       true     。     obj1     "x"  ,      ,obj1     "x"  ,   obj2  "x"           obj1 "x"    ,      “   ” 。 1、                    ,           : 1. $ = { 2. extend : function(target, options) { 3. for (name in options) { 4. target[name] = options[name]; 5. } 6. return target; 7. } 8. };          options       target 。                ,          (     js   “jquery-extend.js”): 1. <script type="text/javascript" src="jquery-extend.js"></script> 2. <script> 3. obj1 = { a : 'a', b : 'b' }; 4. obj2 = { x : { xxx : 'xxx', yyy : 'yyy' }, y : 'y' }; 5. 6. $.extend(obj1, obj2); 7. 8. alert(obj1.x.xxx); //   "xxx" 9. 10.obj2.x.xxx = 'zzz'; 11.alert(obj2.x.xxx); //   "zzz" 12.alert(obj1.x.xxx); //   "zzz" 13.</script>   obj1    "x"  ,          , obj2  "x"        obj1,             。 2、                 “   ”,               ,       extend。     “   ”     : 1. $ = { 2. extend : function(deep, target, options) { 3. for (name in options) { 4. copy = options[name]; 5. if (deep && copy instanceof Array) { 6. target[name] = $.extend(deep, [], copy); 7. } else if (deep && copy instanceof Object) { 8. target[name] = $.extend(deep, {}, copy); 9. } else { 10. target[name] = options[name]; 11. } 12. } 13. return target; 14. } 15.};         :   1.       ,  target[name]       ,      extend;   2.       ,  target[name]       ,      extend; 3.   ,      。       : 1. <script type="text/javascript" src="jquery-extend.js"></script> 2. <script> 3. obj1 = { a : 'a', b : 'b' }; 4. obj2 = { x : { xxx : 'xxx', yyy : 'yyy' }, y : 'y' }; 5. $.extend(true, obj1, obj2); 6. alert(obj1.x.xxx); //   "xxx" 7. obj2.x.xxx = 'zzz'; 8. alert(obj2.x.xxx); //   "zzz" 9. alert(obj1.x.xxx); //   "xxx" 10.</script>               , obj2       obj1     ;             ,  “instanceof Array” IE5           。jQuery             。 3、               jQuery  extend()      : 11.$ = function() { 12. var copyIsArray, 13. toString = Object.prototype.toString, 14. hasOwn = Object.prototype.hasOwnProperty; 15. 16. class2type = { 17. '[object Boolean]' : 'boolean', 18. '[object Number]' : 'number', 19. '[object String]' : 'string', 20. '[object Function]' : 'function', 21. '[object Array]' : 'array', 22. '[object Date]' : 'date', 23. '[object RegExp]' : 'regExp', 24. '[object Object]' : 'object' 25. }, 26. 27. type = function(obj) { 28. return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; 29. }, 30. 31. isWindow = function(obj) { 32. return obj && typeof obj === "object" && "setInterval" in obj; 33. }, 34. 35. isArray = Array.isArray || function(obj) { 36. return type(obj) === "array"; 37. }, 38. 39. isPlainObject = function(obj) { 40. if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) { 41. return false; 42. } 43. 44. if (obj.constructor && !hasOwn.call(obj, "constructor") 45. && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { 46. return false; 47. } 48. 49. var key; 50. for (key in obj) { 51. } 52. 53. return key === undefined || hasOwn.call(obj, key); 54. }, 55. 56. extend = function(deep, target, options) { 57. for (name in options) { 58. src = target[name]; 59. copy = options[name]; 60. 61. if (target === copy) { continue; } 62. 63. if (deep && copy 64. && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) { 65. if (copyIsArray) { 66. copyIsArray = false; 67. clone = src && isArray(src) ? src : []; 68. 69. } else { 70. clone = src && isPlainObject(src) ? src : {}; 71. } 72. 73. target[name] = extend(deep, clone, copy); 74. } else if (copy !== undefined) { 75. target[name] = copy; 76. } 77. } 78. 79. return target; 80. }; 81. 82. return { extend : extend }; 83.}();       $ = function(){...}();    ,             : 1. func = function(){...}; 2. $ = func();            ,      $。        function      ,                。  ,          $.extend(),           ,             extend: 1. return { extend : extend };      ,    extend        ,        : 1. if (target === copy) { continue; }             ,      copy target    ,    “  ”   “     ”,           。                  : 1. type = function(obj) { 2. return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; 3. }, 4. isArray = Array.isArray || function(obj) { 5. return type(obj) === "array"; 6. }            Array.isArray  ,             ,       String,    "[object Array]"。           isPlainObject   : 1. if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) { 2. return false; 3. }        obj.nodeType,      DOM  ;                  :   1.    undefined;   2.   String   "[object Object]";   3. obj   DOM  ;   4. obj window。        DOM   window     ,               ;   window  ,                 ,          。                 : 1. if (obj.constructor && !hasOwn.call(obj, "constructor") 2. && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { 3. return false; 4. }             ,         ,           prototye    ,           。                  : 1. var key; 2. for (key in obj) { 3. } 4. 5. return key === undefined || hasOwn.call(obj, key);                        ,         ,            ,                      。             prototype             ,           ;                  ,                                  ,          ,        "PlainObject"。                : 1. <script type="text/javascript" src="jquery-1.5.2.js"></script> 2. <script> 3. function O() { 4. this.yyy = 'yyy'; 5. } 6. 7. function X() { 8. this.xxx = 'xxx'; 9. } 10. 11.X.prototype = new O(); 12. 13.x = new X(); 14. 15.obj1 = { a : 'a', b : 'b' }; 16.obj2 = { x : x }; 17.$.extend(true, obj1, obj2); 18. 19.alert(obj1.x.yyy); //   "xxx" 20.obj2.x.yyy = 'zzz'; 21.alert(obj1.x.yyy); //   "zzz" 22.</script>       ,            。     ,jQuery  extend()     ,           ,      ,               。  、jQuery             ,  jQuery.extend jQuery.fn.extend: 1. jQuery.extend({ 2. sayhello:function(){ 3. console.log("Hello,This is jQuery Library"); 4. } 5. }) 6. $.sayhello(); //Hello, This is jQuery Library 7. 8. jQuery.fn.extend({ 9. check: function() { 10. return this.each(function() { 11. this.checked = true; 12. }); 13. }, 14. uncheck: function() { 15. return this.each(function() { 16. this.checked = false; 17. }); 18. } 19.}) 20.$( "input[type='checkbox']" ).check(); //   checkbox      1、extend            1. jQuery.extend = jQuery.fn.extend = function() { 2. var options, name, src, copy, copyIsArray, clone, 3. target = arguments[0] || {}, 4. i = 1, 5. length = arguments.length, 6. deep = false; 7. 8. // Handle a deep copy situation 9. if ( typeof target === "boolean" ) { 10. deep = target; 11. target = arguments[1] || {}; 12. // skip the boolean and the target 13. i = 2; 14. } 15. 16. // Handle case when target is a string or something (possible in deep copy) 17. if ( typeof target !== "object" && !jQuery.isFunction(target) ) { 18. target = {}; 19. } 20. 21. // extend jQuery itself if only one argument is passed 22. if ( length === i ) { 23. target = this; 24. --i; 25. } 26. 27. for ( ; i < length; i++ ) { 28. // Only deal with non-null/undefined values 29. if ( (options = arguments[ i ]) != null ) { 30. // Extend the base object 31. for ( name in options ) { 32. src = target[ name ]; 33. copy = options[ name ]; 34. 35. // Prevent never-ending loop 36. if ( target === copy ) { 37. continue; 38. } 39. 40. // Recurse if we're merging plain objects or arrays 41. if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { 42. if ( copyIsArray ) { 43. copyIsArray = false; 44. clone = src && jQuery.isArray(src) ? src : []; 45. 46. } else { 47. clone = src && jQuery.isPlainObject(src) ? src : {}; 48. } 49. 50. // Never move original objects, clone them 51. target[ name ] = jQuery.extend( deep, clone, copy ); 52. 53. // Don't bring in undefined values 54. } else if ( copy !== undefined ) { 55. target[ name ] = copy; 56. } 57. } 58. } 59. } 60. 61. // Return the modified object 62. return target; 63.};             jQuery.extend()            ,      ,        , extend              ,      : 1. jQuery.extend = jQuery.fn.extend = function(obj){ 2. //obj        this     3. var target=this; 4. for (var name in obj){ 5. //name      6. //copy     7. copy=obj[name]; 8. //       9. if(target === copy) continue; 10. //         11. if(typeof copy === 'undefined') continue; 12. //   13. target[name]=copy; 14. } 15. return target; 16.} 2、extend        : 1. jQuery.extend = jQuery.fn.extend = function() { 2. //           3. //                 4. //options             5. //name            6. //i            7. //deep        8. var options, name, src, copy, copyIsArray, clone, 9. target = arguments[0] || {}, 10. i = 1, 11. length = arguments.length, 12. deep = false; 13. 14. //            ,            15. //            16. if ( typeof target === "boolean" ) { 17. deep = target; 18. target = arguments[1] || {}; 19. //          ,         20. i = 2; 21. } 22. 23. //               ,            24. if ( typeof target !== "object" && !jQuery.isFunction(target) ) { 25. target = {}; 26. } 27. 28. //          ,       jQuery jQuery.fn 29. if ( length === i ) { 30. target = this; 31. --i; 32. } 33. 34. //  i            35. for ( ; i < length; i++ ) { 36. //          37. if ( (options = arguments[ i ]) != null ) { 38. //        39. for ( name in options ) { 40. src = target[ name ]; 41. copy = options[ name ]; 42. 43. //        44. if ( target === copy ) { 45. continue; 46. } 47. 48. //         49. if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { 50. if ( copyIsArray ) { 51. copyIsArray = false; 52. clone = src && jQuery.isArray(src) ? src : []; 53. 54. } else { 55. clone = src && jQuery.isPlainObject(src) ? src : {}; 56. } 57. 58. target[ name ] = jQuery.extend( deep, clone, copy ); 59. 60. //         61. } else if ( copy !== undefined ) { 62. // target        63. target[ name ] = copy; 64. } 65. } 66. } 67. } 68. 69. //   70. return target; 71.}; 

좋은 웹페이지 즐겨찾기