|
@@ -0,0 +1,111 @@
|
|
1
|
+
|
|
2
|
+var getProto = Object.getPrototypeOf || function (obj) {
|
|
3
|
+ /*jshint proto: true */
|
|
4
|
+ return obj.__proto__;
|
|
5
|
+};
|
|
6
|
+var hasOwn = Object.prototype.hasOwnProperty;
|
|
7
|
+var fnToString = hasOwn.toString;
|
|
8
|
+var ObjectFunctionString = fnToString.call(Object);
|
|
9
|
+
|
|
10
|
+/**
|
|
11
|
+ * 测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)。
|
|
12
|
+ */
|
|
13
|
+var isPlainObject = function(obj) {
|
|
14
|
+ var proto, Ctor;
|
|
15
|
+
|
|
16
|
+ // Detect obvious negatives
|
|
17
|
+ // Use toString instead of jQuery.type to catch host objects
|
|
18
|
+ if (!obj || toString.call(obj) !== "[object Object]") {
|
|
19
|
+ return false;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ proto = getProto(obj);
|
|
23
|
+
|
|
24
|
+ // Objects with no prototype (e.g., `Object.create( null )`) are plain
|
|
25
|
+ if (!proto) {
|
|
26
|
+ return true;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ // Objects with prototype are plain iff they were constructed by a global Object function
|
|
30
|
+ Ctor = hasOwn.call(proto, "constructor") && proto.constructor;
|
|
31
|
+ return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString;
|
|
32
|
+};
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+var isFunction = function (val) {
|
|
36
|
+ return (typeof val === 'function');
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+var extend = function () {
|
|
41
|
+ var options, name, src, copy, copyIsArray, clone,
|
|
42
|
+ target = arguments[0] || {},
|
|
43
|
+ i = 1,
|
|
44
|
+ length = arguments.length,
|
|
45
|
+ deep = false;
|
|
46
|
+
|
|
47
|
+ // Handle a deep copy situation
|
|
48
|
+ if (typeof target === "boolean") {
|
|
49
|
+ deep = target;
|
|
50
|
+
|
|
51
|
+ // Skip the boolean and the target
|
|
52
|
+ target = arguments[i] || {};
|
|
53
|
+ i++;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ // Handle case when target is a string or something (possible in deep copy)
|
|
57
|
+ if (typeof target !== "object" && !isFunction(target)) {
|
|
58
|
+ target = {};
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ // Extend jQuery itself if only one argument is passed
|
|
62
|
+ if (i === length) {
|
|
63
|
+ target = this;
|
|
64
|
+ i--;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ for (; i < length; i++) {
|
|
68
|
+
|
|
69
|
+ // Only deal with non-null/undefined values
|
|
70
|
+ if ((options = arguments[i]) != null) {
|
|
71
|
+
|
|
72
|
+ // Extend the base object
|
|
73
|
+ for (name in options) {
|
|
74
|
+ src = target[name];
|
|
75
|
+ copy = options[name];
|
|
76
|
+
|
|
77
|
+ // Prevent never-ending loop
|
|
78
|
+ if (target === copy) {
|
|
79
|
+ continue;
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ // Recurse if we're merging plain objects or arrays
|
|
83
|
+ if (deep && copy && (isPlainObject(copy) ||
|
|
84
|
+ (copyIsArray = Array.isArray(copy)))) {
|
|
85
|
+
|
|
86
|
+ if (copyIsArray) {
|
|
87
|
+ copyIsArray = false;
|
|
88
|
+ clone = src && Array.isArray(src) ? src : [];
|
|
89
|
+
|
|
90
|
+ } else {
|
|
91
|
+ clone = src && isPlainObject(src) ? src : {};
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ // Never move original objects, clone them
|
|
95
|
+ target[name] = extend(deep, clone, copy);
|
|
96
|
+
|
|
97
|
+ // Don't bring in undefined values
|
|
98
|
+ } else if (copy !== undefined) {
|
|
99
|
+ target[name] = copy;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ // Return the modified object
|
|
106
|
+ return target;
|
|
107
|
+};
|
|
108
|
+
|
|
109
|
+module.exports = {
|
|
110
|
+ extend: extend
|
|
111
|
+}
|