function clone(obj) { var newObj; if (typeof obj === 'object') { if (Object.prototype.toString.call(obj) === '[object Array]') { newObj = []; var i = obj.length; while (i--) { newObj[i] = this.clone(obj[i]); } return newObj; } else { newObj = {}; for (var j in obj) { newObj[j] = this.clone(obj[j]); } } } else { return obj; } return newObj; }
jquery参考如下:
参考: http://www.css88.com/jqapi-1.9/jQuery.extend/
// 浅层复制(只复制顶层)
var newObject = jQuery.extend({}, oldObject);
// 深层复制(递归复制)
var newObject = jQuery.extend(true, {}, oldObject);