function clone(obj) {
var o;
if (typeof obj == "object") {
if (obj === null) {
o = null;
} else {
// instanceof检测对象A是不是对象B的实例,原理是查看对象B的prototype指向的对象是否在对象A的[[prototype]]链上,如果在,则返回true
if (obj instanceof Array) {
o = [];
for (var i = 0, len = obj.length; i < len; i++) {
o.push(clone(obj[i]));
}
} else {
o = {};
for (var j in obj) {
o[j] = clone(obj[j]);
}
}
}
} else {
o = obj;
}
return o;
}