深度克隆注意事项

1.传参(如果参数为空时)

2.事件名(如果突然事件名要重新命名时)

function cloneObject(json){

  if(json===null || arguments.length==0){//如果对象为空或者无参数时返回null

    return null;

  }

  var o=json.constructor===Array?[]:{};//判断参数是数组还是json(对象)

  for(var i in json){//循环对象进行复制

    if(json.hasOwnProperty(i)){//当数组或者json(对象)有值的时候才进行克隆

      o[i]=typeof json[i]==="object" ? arguments.callee(json[i]):json[i];//如果是对象就执行递归克隆,不是则执行直接复制

    }

  }

  return o;

}

posted on 2014-02-08 15:14  apple_半个苹果  阅读(168)  评论(0编辑  收藏  举报