Skip to content

Instantly share code, notes, and snippets.

@think2011
Created May 12, 2017 10:14

Revisions

  1. think2011 created this gist May 12, 2017.
    15 changes: 15 additions & 0 deletions cloneObj.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var cloneObj = function(obj){
    var str, newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object'){
    return;
    } else if(window.JSON){
    str = JSON.stringify(obj), //系列化对象
    newobj = JSON.parse(str); //还原
    } else {
    for(var i in obj){
    newobj[i] = typeof obj[i] === 'object' ?
    cloneObj(obj[i]) : obj[i];
    }
    }
    return newobj;
    };