function DeepClone (data) {
if (data === null || typeof data !== 'object') return data;
var newObj = data instanceof Array ? [] : {};
for (var key in data) newObj[key] = DeepClone(data[key]);
return newObj;
}
var obj = {
id: 1,
name: 'xxx',
sayName: function (a) {
console.log('my name is ' + a);
},
childs: [
{'a': 1},
{'c': 'd'},
],
opts: {
xxx: [1,2,'3'],
aa: 'bb'
},
};
var newObj = DeepClone(obj);
console.log(obj)
console.log(newObj)
obj.sayName('bior')
newObj.sayName('帅哥')
注意: 此方法有点问题... 忘记考虑到特殊对象(formdata 和 date 等),还是得多判断一步,太懒了,看一下我的另外一篇文章吧:
https://www.jianshu.com/p/a1b76b4e24a8
网友评论