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;
};
function Owner(name) {
this.name = name;
}
function Car(owner, brand) {
this.owner = owner;
this.brand = brand;
this.drive = function() {
alert(this.owner.name + "开着" + brand + "飞奔");
};
}
var o = new Owner("Hao");
var c1 = new Car(o, "Benz");
c1.drive();
var c2 = c1.clone();
c2.owner.name = "Wang";
c1.drive();