首页 > 这段js对象深拷贝的代码有啥问题?

这段js对象深拷贝的代码有啥问题?


如题,能帮忙指出下错误么?

function cloneObj(obj) {
    if(typeof obj !== "object"){
        return obj;
    }
    var s = {};
    if(obj.constructor == Array){
        s = [];
    }
    for(var i in obj){
        s[i] = cloneObj(obj[i]);
    }
    return s;
}

--------------------------------------------------------------------------------------

找到问题所在了, 当obj为null的时候就会报错

![图片描述][1]

    if (Array.isArray(obj))

或者

    if (obj instanceof Array)

也是一种选择


if( Object.prototype.toString.call( obj ) === '[object Array]' ) 

这个需要把不可枚举属性排除吧?

【热门文章】
【热门文章】