用jQuery和JavaScript深度复制JSON对象的方法
jopen
9年前
你有时可能需要复制一个JSON对象,jQuery提供了的extend方法以前是无法进行深度复制的,不过现在也已经支持了:
</div> </div>
来自:http://ourjs.com/detail/55c061d3fbd23139de9e3551
// Shallow copy var newObject = jQuery.extend({}, oldObject); // Deep copy var newObject = jQuery.extend(true, {}, oldObject);
StackOverFlow的一位网友提供了一个深度复制的函数
Array.prototype.clone = function(doDeepCopy) {if(doDeepCopy) {var encountered = [{a : this,b : []}];var item,levels = [{a:this, b:encountered[0].b, i:0}],level = 0,i = 0,len = this.length;while(i < len) {item = levels[level].a[i];if(Object.prototype.toString.call(item) === "[object Array]") {for(var j = encountered.length - 1; j >= 0; j--) {if(encountered[j].a === item) {levels[level].b.push(encountered[j].b);break;}}if(j < 0) {encountered.push(j = {a : item,b : []});levels[level].b.push(j.b);levels[level].i = i + 1;levels[++level] = {a:item, b:j.b, i:0};i = -1;len = item.length;}}else {levels[level].b.push(item);}if(++i == len && level > 0) {levels.pop();i = levels[--level].i;len = levels[level].a.length;}}return encountered[0].b;}else {return this.slice(0);}};
不过此函数是加到Array对象原型上的,这意味着你使用 for in 枚举array数组时也会枚举出这个方法。这里有一个函数版本:
function clone(item) {if (!item) { return item; } // null, undefined values checkvar types = [ Number, String, Boolean ],result;// normalizing primitives if someone did new String('aaa'), or new Number('444');types.forEach(function(type) {if (item instanceof type) {result = type( item );}});if (typeof result == "undefined") {if (Object.prototype.toString.call( item ) === "[object Array]") {result = [];item.forEach(function(child, index, array) {result[index] = clone( child );});} else if (typeof item == "object") {// testing that this is DOMif (item.nodeType && typeof item.cloneNode == "function") {var result = item.cloneNode( true );} else if (!item.prototype) { // check that this is a literalif (item instanceof Date) {result = new Date(item);} else {// it is an object literalresult = {};for (var i in item) {result[i] = clone( item[i] );}}} else {// depending what you would like here,// just keep the reference, or create new objectif (false && item.constructor) {// would not advice to do that, reason? Read belowresult = new item.constructor();} else {result = item;}}} else {result = item;}}return result;}var copy = clone({one : {'one-one' : new String("hello"),'one-two' : ["one", "two", true, "four"]},two : document.createElement("div"),three : [{name : "three-one",number : new Number("100"),obj : new function() {this.name = "Object test";}}]})
其实深度复制JavaScript对象还有一个更加简单的方法, 一行代码即可,比如:
var cloned = JSON.parse(JSON.stringify(objectToClone));
原文地址: stackoverflow.com
</div> 来自:http://ourjs.com/detail/55c061d3fbd23139de9e3551