Created
April 9, 2013 07:43
-
-
Save ziluo/5343757 to your computer and use it in GitHub Desktop.
对象的深度复制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.prototype.Clone = function(){ | |
var objClone; | |
if (this.constructor == Object){ | |
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].Clone(); | |
}else{ | |
objClone[key] = this[key]; | |
} | |
} | |
} | |
objClone.toString = this.toString; | |
objClone.valueOf = this.valueOf; | |
return objClone; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment