jQuery中extend实现深拷贝和浅拷贝

2016年10月06日Web前端

在jQuery中,我们可以使用$.extend方法简单的实现对象的拷贝。

一、extend浅拷贝

var a = {
    name: 'aa',
    age: 12,
    friends: [{
        name: 'Jack',
        age: 12
    }, {
        name: 'Tom',
        age: 13
    }],
    con: {
        height: '160cm',
        weight: '61kg'
    }
};
var b = {
    name: 'bb',
    age: 14,
    friends: [{
        name: 'Amy',
        age: 16
    }],
    con: {
        height: '154cm',
        weight: '56kg'
    }
};

var c = $.extend(a, b);
console.log(JSON.stringify(a));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(b));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(c));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}

extend默认是浅拷贝,在此,我们发现,是把b的值拷贝到a上,并返回了a,接着看,这几者之间的关系,还是那两个变量a与b:

var c = $.extend(a, b);
b.name = 'cccc';
b.con.weight= '70kg';
console.log(JSON.stringify(a));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"70kg"}}
console.log(JSON.stringify(b));
// {"name":"cccc","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"70kg"}}
console.log(JSON.stringify(c));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"70kg"}}

因此我们可以得知浅拷贝在拷贝内层对象时是才用引用的方式

var c = $.extend(a, b);
a.name = 'AAA';
a.con.height = '170cm';
console.log(JSON.stringify(a));
// {"name":"AAA","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"170cm","weight":"56kg"}}
console.log(JSON.stringify(b));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"170cm","weight":"56kg"}}
console.log(JSON.stringify(c));
// {"name":"AAA","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"170cm","weight":"56kg"}}

由此,我们发现其实c是对a的一个引用。当然,如果我们不想串改变量a中的值,我们可以采用:

var c = $.extend({}, a, b);
console.log(JSON.stringify(a));
// {"name":"aa","age":12,"friends":[{"name":"Jack","age":12},{"name":"Tom","age":13}],"con":{"height":"160cm","weight":"61kg"}}
console.log(JSON.stringify(b));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(c));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}

这样的话,变量a中的值就不会被拷贝而被改变了。

二、extend深拷贝

依旧采用浅拷贝中的两个变量a与变量b,

var c = $.extend(true, a, b);
console.log(JSON.stringify(a));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16},{"name":"Tom","age":13}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(b));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(c));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16},{"name":"Tom","age":13}],"con":{"height":"154cm","weight":"56kg"}}

可以发现,得到的变量c中包含部分变量a和变量b中的值,继续对变量b操作,

var c = $.extend(true, a, b);
b.name = 'cccc';
b.con.weight= '70kg';
console.log(JSON.stringify(a));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16},{"name":"Tom","age":13}],"con":{"height":"154cm","weight":"56kg"}}
console.log(JSON.stringify(b));
// {"name":"cccc","age":14,"friends":[{"name":"Amy","age":16}],"con":{"height":"154cm","weight":"70kg"}}
console.log(JSON.stringify(c));
// {"name":"bb","age":14,"friends":[{"name":"Amy","age":16},{"name":"Tom","age":13}],"con":{"height":"154cm","weight":"56kg"}}

我们发现,修改拷贝前变量第二层对象中的值时,不会反应到拷贝后的变量中的值。

当然,和浅拷贝一样,为了使得变量a不变,可以使用

var c = $.extend(true, {}, a, b);

三、总结

当我们使用extend来实现拷贝时,使用浅拷贝时,只会将对象内一层的变量进行直接赋值,如果对象中嵌套了对象,内部的对象就会被直接赋值,形成引用赋值,修改变量就会引起其他变量的变化。为了能够拷贝多层变量,我们可以使用深拷贝,当然如果两拷贝对象中有相同的属性名时,后者的值也会覆盖前者的值。