/** * 克隆对象 */ //函数法 function cloneObj(myObj){ if(typeof(myObj) != 'object') { return myObj; } if(myObj == null) { return myObj; } if(myObj instanceof Array ){ var myNewObj = []; }else{ var myNewObj = new Object(); } for(var i in myObj) { myNewObj[i] = cloneObj(myObj[i]); } return myNewObj; } //原型链法 Object.prototype.objectCopy = function() { var objClone; if ( this.constructor == Object ) { if(this.constructor instanceof Array){ objClone = []; }else{ objClone = new this.constructor(); } } else { objClone = new this.constructor(this.valueOf()); } for ( var key in this ) { if ( objClone[key] != this[key] ) { if ( typeof(this[key]) == 'object' ) { objClone[key] = this[key].objectCopy(); } else { objClone[key] = this[key]; } } } objClone.toString = this.toString; objClone.valueOf = this.valueOf; return objClone; }