var src = {
src: 'SRC',
test: {
name: 'test',
val: 0
}
};
var dist1 = _.extend({ dist: 1 }, src);
dist1.test.name = 'test1';
dist1.test.val = 1;
console.log('src', src);
console.log('dest1', dist1);
var dist2 = _.extend({}, src);
dist2.dist = 2;
dist2.test.name = 'test2';
console.log('src', src);
console.log('dest2', dist2);
var dist3 = _.clone(src);
dist3.dist = 3;
console.log('src', src);
console.log('dest3', dist3);
运行这段代码发现,test其实是同一个引用
这样不是一不小心就会产生bug?
试试 Lodash,兼容underscore,并且优化了性能,同时对api进行了扩展
里面就有
_.clone()
_.cloneDeep()
详见