题解 | #浅拷贝#

浅拷贝

http://www.nowcoder.com/practice/434d236e52994a9791a055f1f7adf985

const _shallowClone = target => {
  if(typeof target == "object" &&  target !== null){                       // Array,null 的 type 类型也是 object
        if(/^(RegExp|Date|Set|Map)$/.test(target.constructor.name)){       // 使用构造函数名,也是实例化对象名进行第一步筛选
          return target;                                                     
        }
        let collect = target instanceof Array ? [] : {};                   // 剩下的是数组和对象
        for( prop in target){
          if(target.hasOwnProperty(prop))
            collect[prop] = target[prop]
        }
        return collect;
  }else{
    	return target                  									   // 剩下的是number,bollean,string,undefined等数据类型
  }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 14:35
点赞 评论 收藏
分享
点赞 评论 收藏
分享
缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务