使用Object.create()方法
直接使用var newObj = Object.create(oldObj),可以达到深拷贝的效果。
/* ================ 深拷贝 ================ */
function
deepClone(initalObj, finalObj) {
var
obj = finalObj || {};
for
(
var
i
in
initalObj) {
var
prop = initalObj[i];
// 避免相互引用对象导致死循环,如initalObj.a = initalObj的情况
if
(prop === obj) {
continue
;
}
if
(
typeof
prop ===
'object'
) {
obj[i] = (prop.constructor === Array) ? [] : Object.create(prop);
}
else
{
obj[i] = prop;
}
}
return
obj;
}
https://www.jb51.net/article/99013.htm