js valueof与toSting,对象的复制
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
本文来自博客园,作者:至道中和,转载请注明原文链接:https://www.cnblogs.com/voidobject/archive/2012/12/27/3975485.html

浙公网安备 33010602011771号