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;
};
JavaScript的对象深度克隆方法
最新推荐文章于 2022-06-24 12:48:00 发布