玩玩儿对象的深拷贝
[javascript]
Object.prototype.clone = function () {
if (typeof(this) != 'object') return this;
if (this == null) return this;
var o = Object.prototype.toString.call(this) === '[object Array]' ? [] : {};
for (var i in this) {
if (typeof this[i] === 'object' && this !== this[i]) {
o[i] = this[i].clone();
} else o[i] = this[i];
}
return o;
}
//test-----------------------------------------------------------------------
var a = {
c: 'testc'
};
a.b = a;
var b = a.clone();
console.log(b);
[/javascript]
讨论地址:http://bbs.51js.com/viewthread.php?tid=87563&extra=page%3D1