玩玩 javascript 对象clone 与复制。

var a = {
    name:"wangliang",
    age:"29"
}


var b = (function(old) {
    var _b={};
    var key;

    for (key in old) {
        if(a.hasOwnProperty(key)){
        _b[key] = a[key]; //拷贝属性
        }
    }
    return _b;
}(a));

b.name = "wl"

console.log(a.name);// "wangliang"


你可能感兴趣的:(玩玩 javascript 对象clone 与复制。)