码迷,mamicode.com
首页 > 编程语言 > 详细

深度克隆(对象、数组)--------百度IFE前端task2

时间:2016-01-05 18:45:32      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

var srcObj = {
  a: 1,
  b: {
    b1: ["hello", "hi"],
    b2: "JavaScript"
  }
};

console.log(srcObj);
var clone1 = cloneObject(srcObj);

function cloneObject(src) {
  var clone = {};
  for(var item in src){
    if(typeof(src[item])==‘object‘ ){
      if(src[item] instanceof Array){
        clone[item] = [];
        for(var i=0,len=src[item].length; i<len; i++){
          clone[item][i] = src[item][i];
        }
      } else {
        clone[item] = cloneObject(src[item]);
      }
    } else{
      clone[item] = src[item];
    }
  }
  return clone;
}

console.log(clone1);

深度克隆(对象、数组)--------百度IFE前端task2

标签:

原文地址:http://www.cnblogs.com/dxiaoer/p/5103025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!