export function clone(val) {
let k, out, tmp;
if (Array.isArray(val)) {
out = Array(k = val.length);
while (k--) out[k] = (tmp = val[k]) && typeof tmp === 'object' ? clone(tmp) : tmp;
return out;
}
if (Object.prototype.toString.call(val) === '[object Object]') {
out = {}; // null
for (k in val) {
if (k === '__proto__') {
Object.defineProperty(out, k, {
value: clone(val[k]),
configurable: true,
enumerable: true,
writable: true
});
} else if (typeof val[k] !== 'function') { // MODIFICATION: skip functions
out[k] = (tmp = val[k]) && typeof tmp === 'object' ? clone(tmp) : tmp;
}
}
return out;
}
return val;
}
CloneValfunc
CodingBookThe function clone creates a copy of an object. It accepts an object as its only parameter. If the object is an array, the function returns an Array object. It uses a while loop to iterate through the length of the object and creates an Array object each time it is iterated over. The Array object contains the same items as the original object, but it is created using the clone() method. The cloned objects have the same properties as the original object, but they are not clones of the original object's prototype properties. The clone() method can be used to create a clone of a function, as in this example.
Shortcut: cloneVal.Func
0 Comments
Add Comment
Log in to add a comment