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;
}
valueof 与toString() 区别
var obj = {
i: 10,
valueOf: function () { return this.i + 30; },
toString: function () { return this.valueOf() + 10; }
}
alert(obj > 20); // true
alert(+obj); // 40
alert(obj); // 50