-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend.js
More file actions
34 lines (30 loc) · 732 Bytes
/
extend.js
File metadata and controls
34 lines (30 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// The function that copies objects and their properties.
// It function doesn't work with prototypes.
// A shallow copy.
const extend = (parent, child = {}) {
const i;
for(i in parent) {
if(parent.hasOwnProperty(i)) {
child[i] = parent[i]
}
}
return child
}
// A full copy.
function extendDeep(parent, child) {
var i;
var toStr = Object.prototype.toString;
var astr = '[object Array]';
child = child || {};
for(i in parent) {
if (parent.hasOwnProperty(i)) {
if (typeof parent[i] === 'object') {
child[i] = (toStr.call(parent[i]) === astr) ? [] : {};
extendDeep(parent[i], child[i]);
} else {
child[i] = parent[i];
}
}
}
return child;
}