码迷,mamicode.com
首页 > 其他好文 > 详细

$.extend

时间:2017-05-02 21:02:06      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:nbsp   type   bool   foreach   isp   span   end   cal   div   

通过源对象扩展目标对象的属性,源对象属性将覆盖目标对象属性。

默认情况下为,复制为浅复制。如果第一个参数为true表示深度复制

 1 $.extend = function (target) {
 2             var deep, args = slice.call(arguments, 1)
 3             if (typeof target == ‘boolean‘) {
 4                 deep = target
 5                 target = args.shift()
 6             }
 7             args.forEach(function (arg) {
 8                 extend(target, arg, deep)
 9             })
10             return target
11         }
12 
13 function extend(target, source, deep) {
14             for (key in source)
15                 if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
16                     if (isPlainObject(source[key]) && !isPlainObject(target[key]))
17                         target[key] = {}
18                     if (isArray(source[key]) && !isArray(target[key]))
19                         target[key] = []
20                     extend(target[key], source[key], deep)
21                 }
22                 else if (source[key] !== undefined) target[key] = source[key]
23         }

 

$.extend

标签:nbsp   type   bool   foreach   isp   span   end   cal   div   

原文地址:http://www.cnblogs.com/sunnie-cc/p/6797977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!