深くコピーして終わり編

3359 ワード

const getType=Object.prototype.toString.call(obj);
const isObject=>(type of target===object'|type of target==='function')&target!=====null
const canTraverse=''object Map':true,'[object Set]::true,'[object Aray]::true,'[object Object]':true,'[oject Agments]::true,';const mapTag='[object Map]'const set Tag='[object Set]'const book='[object Boolean]'const number Tag='[object Number]'const stregTag='[object String]'const smbol Tag='[object Symbol]'const dateTag='[object Date]'const errorTag='[object Err]'const regexpTag='[object RegExp]'const funcTag='[object Funct]'
const handle RegExp=>{const{source、flags}=target;return new target.com nstructor;
const handleFunnc=="""//矢印関数は直接に自身に戻るif(!func.prototype) return func;const bodReg="""""""""""""""""""""""""""="""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""");const body=bodyReg.exec(funcString);if(!body)return null;if(param){
const paramArr = param[0].split(',');
return new Function(...paramArr, body[0]);
}else{
return new Function(body[0]);
}
const handleNot Traverse="{const Ctor=target.com nstructor;switch.}{
case boolTag:
  return new Object(Boolean.prototype.valueOf.call(target));
case numberTag:
  return new Object(Number.prototype.valueOf.call(target));
case stringTag:
  return new Object(String.prototype.valueOf.call(target));
case symbolTag:
  return new Object(Symbol.prototype.valueOf.call(target));
case errorTag: 
case dateTag:
  return new Ctor(target);
case regexpTag:
  return handleRegExp(target);
case funcTag:
  return handleFunc(target);
default:
  return new Ctor(target);
}
const deepClone=(targt,map=new WeakMap()=>{if(!isObject(target)
return target;
let type=getType;let clone Target;if(!canTraverse[type])
//          
return handleNotTraverse(target, type);
}else{
//         ,            !
let ctor = target.constructor;
cloneTarget = new ctor();
)
if(map.get)
return target;
map.set(target,true)
if(type==mapTag){
//  Map
target.forEach((item, key) => {
  cloneTarget.set(deepClone(key, map), deepClone(item, map));
})
)
if(type==setTag){
//  Set
target.forEach(item => {
  cloneTarget.add(deepClone(item, map));
})
)
//処理配列とオブジェクトfor(let prop in target){
if (target.hasOwnProperty(prop)) {
    cloneTarget[prop] = deepClone(target[prop], map);
}
}return clone Target;