function cloneObj(source, target) {
if (!target) {
if (source.constructor === RegExp) {
target = new RegExp(source.source, source.flags);
} else {
target = new source.constructor();
}
}
var names = Object.getOwnPropertyNames(source);
for (var i = 0; i < names.length; i++) {
var desc = Object.getOwnPropertyDescriptor(source, names[i]);
if (typeof desc.value === "object" && desc.value !== null) {
var values = cloneObj(desc.value);
Object.defineProperty(target, names[i], {
enumerable: desc.enumerable,
configurable: desc.configurable,
writable: desc.writable,
value: values
});
} else {
Object.defineProperty(target, names[i], desc);
}
}
return target;
}
注:这个方法不能复制DOM和二进制流。