兼容值类型的JavaScript对象Clone方法 原贴地址在这里 // Authors Birdshome, 麻袋@博客园 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; } //测试代码 function MyObj(){ this.a = '123'; this.b = 2; this.toString = function(){ return 'a: ' + this.a + '/n' + 'b: ' + this.b + '/n'; }; }; var o1 = new MyObj(); //alert(typeof o1); var o2 = o1.Clone(); alert(o2.toString());