$.extend(obj1,0bj2,{"name":"s","age":22})
//target 要拷贝到哪个对象上
// i 要执行拷贝的次数
// length 要拷贝的参数的长度
// name 对象参数中属性值
// options 对象参数
// clone 深度拷贝时重名对象属性的拷贝
// target 要拓展的对象 jQuery.extend = jQuery.fn.extend = function() {
var src, copyIsArray, copy, name, options, clone,
target = arguments[0] || {}, // 常见用法 jQuery.extend( obj1, obj2 ),此时,target为arguments[0]
i = 1,
length = arguments.length,
deep = false; // Handle a deep copy situation
if ( typeof target === "boolean" ) { // 如果第一个参数为true,即 jQuery.extend( true, obj1, obj2 ); 的情况
deep = target; // 此时target是true
target = arguments[1] || {}; // target改为 obj1
// skip the boolean and the target
i = 2;
}
// 以上处理:如果第一个参数是boolean,第二个参数是target。
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) { // 如果target不是一个对象,设置默认对象。
target = {};
} // extend jQuery itself if only one argument is passed
if ( length === i ) { // 处理这种情况 jQuery.extend(obj),或 jQuery.fn.extend( obj )
target = this; // jQuery.extend时,this指的是jQuery;jQuery.fn.extend时,this指的是jQuery.fn
--i;
} for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[i]) != null ) { // 比如 jQuery.extend( , obj2, obj3, ojb4 ),options则为 obj2、obj3...
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ]; // Prevent never-ending loop
if ( target === copy ) { // 防止自引用:$.extend(x,{"name1":x});如果 如果target和option[name1]的值一样,会循环迭代。
//看网上还有说应该要用src和copy比较的,$.extend({"name1":"张三","age":"14"},{"name1":"李四","age":"14"}),这两个可以进行操作的。
continue;
} // Recurse if we're merging plain objects or arrays
// 如果是深拷贝,且被拷贝的属性值本身是个对象
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) { // 被拷贝的属性值是个数组
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : []; } else { //被拷贝的属性值是个plainObject,比如{ nick: 'casper' }
clone = src && jQuery.isPlainObject(src) ? src : {};
} // Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy ); // 递归~ // Don't bring in undefined values
} else if ( copy !== undefined ) { // 浅拷贝,且属性值不为undefined
target[ name ] = copy;
}
}
}
} // Return the modified object
return target; {
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":{
name1:"sdasd",
"name2":
}
}
}
}
}
}
}
}

  

最新文章

  1. poj-1611-The Suspects
  2. POJ1351 Number of Locks(数学)
  3. 18.C#扩展方法(十章10.1-10.2)
  4. android 工程出现感叹号错误
  5. 判断浏览器类型-----------navigator.userAgent.indexOf()
  6. 用定时器T0的中断控制8位LED闪烁
  7. Deep Learning(深度学习) 学习笔记(四)
  8. mysql进阶(二十二)MySQL错误之Incorrect string value: &#39;\xE7\x81\xAB\xE7\x8B\x90...中文字符输入错误
  9. 为什么range不是迭代器?range到底是什么类型?
  10. android系统中如何通过程序打开某个AccessibilityService
  11. Windows 10,鼠标右键-发送到-桌面快捷方式缺失解决方法
  12. 运维grep语法
  13. http://www.cnblogs.com/chenmeng0818/p/6370819.html
  14. 最全面的Android Studio使用教程【申明:来源于网络】
  15. Can&#39;t push you anymore...
  16. python:数据类型
  17. codeforce——因数筛
  18. HDU1671 水题字典树
  19. XML 十六进制值 是无效的字符错误 解决方法之一 转
  20. Linux Kernel ---- PCI Driver 分析

热门文章

  1. WEB前端研发工程师编程能力成长之路(1)
  2. centos6.8安装zabbix
  3. spark 作业提交
  4. googleMapReduce
  5. java并发内存模型
  6. LINUX SHELL 笔记 02: 变量初识
  7. java switch笔记
  8. log4j2按日志级别输出到指定文件
  9. PHP爬取知乎日报图片显示不了问题
  10. C#中【正则表达式regular expression】相关的知识