JavaScript的对象深度克隆方法

来源:互联网 发布:网络是把双刃剑英语 编辑:程序博客网 时间:2024/05/17 20:02
Object.prototype.clone = function() {var newObj = {};for(var i in this) {if(typeof(this[i]) == "object" || typeof(this[i]) == "function") {newObj[i] = this[i].clone();}else {newObj[i] = this[i];}}return newObj;};Array.prototype.clone = function() {var newArray = [];for(var i = 0; i < this.length; i++) {if(typeof(this[i]) == "object" || typeof(this[i]) == "function") {newArray[i] = this[i].clone();}else {newArray[i] = this[i];}}return newArray;};Function.prototype.clone = function() {var that = this;var newFunc = function() {return that.apply(this, arguments);};for(var i in this) {newFunc[i] = this[i];}return newFunc;};


0 0