通过迭代器在javascript中进行深度扩展

通过迭代器在javascript中进行深度扩展,javascript,Javascript,我想使用扩展javascript对象,扩展到所有可能的嵌套级别 我使用这个函数(返回的函数)作为迭代器 var ObjWalk = (function( GLOBAL_APP_CONFIG,GLOBAL_METHODS){ if (typeof GLOBAL_APP_CONFIG !== 'object' || GLOBAL_APP_CONFIG === null) GLOBAL_APP_CONFIG = {}; const maxobjdepth = GLOBAL_APP_CONFIG

我想使用扩展javascript对象,扩展到所有可能的嵌套级别

我使用这个函数(返回的函数)作为迭代器

var ObjWalk = (function( GLOBAL_APP_CONFIG,GLOBAL_METHODS){
  if (typeof GLOBAL_APP_CONFIG !== 'object' || GLOBAL_APP_CONFIG === null) GLOBAL_APP_CONFIG = {};
  const maxobjdepth = GLOBAL_APP_CONFIG.maxobjdepth || 99;
  const endvar = GLOBAL_APP_CONFIG.walkendkey || '$W_END';

  let ifEndForObjWalk = GLOBAL_METHODS && GLOBAL_METHODS.ifEndForObjWalk;
  if(typeof ifEndForObjWalk !== 'function') {
   ifEndForObjWalk = function(obj, depth) {
      return ((depth < maxobjdepth && typeof obj === 'object'
        && obj !== null && obj[endvar] !== true) ? obj : false);
    };
  };

  const walkInto = function(fun, rt, obj, key, depth, isLast) {
    if(!depth) depth = 0;
    fun(obj, key, rt, depth || 0, typeof isLast === 'boolean' ? isLast : true);
    const ob = ifEndForObjWalk(obj, depth);
    if (ob) {
      const kys = Object.keys(ob);
      const lastln = kys.length - 1;
      const deep = depth + 1;
      for (let z = 0; z <= lastln; z += 1) {
        walkInto(fun, ob, ob[kys[z]], kys[z], deep, (z === lastln));
      }
    }
  };

  return walkInto;
})();
这里是一个失败的沙箱

问题出在哪里很难调试。 有人帮忙吗

  const deepExtend = function(obj1, obj2) {
    let ptr1 = obj1;
    // resolve if new value should be array or object or no change
    function resolve(ab, vl){
      if ((typeof vl === 'object')
        && (ab === undefined || typeof ab !== 'object'
          || (vl === null && ab !== null)
          || (vl !== null && ab === null)
          || (Array.isArray(vl) && !Array.isArray(ab))
          || (!Array.isArray(vl) && Array.isArray(ab))
          || (vl.length > ab.length)
        )
      ) {
        ab = vl === null ? vl : (Array.isArray(vl) ? new Array(vl.length) : {});
      }
      return ab;
    }
    ptr1 = resolve(obj1, obj2);
    if (typeof ptr1 !== 'object') return ptr1;
    // ptrs1 = Array of pointers that will make the changes to a particular depth at a time.
    const ptrs1 = [[null,true],[ptr1,true]];
    // below vl = value, ky = key, ob = someOb where someOb[ky] = vl
    // dpt = nested depth
    // isLast = if the `ky` is the last key of someOb
    ObjWalk(function(vl, ky, ob, dpt, isLast){
      if (ky && dpt) {
        // whenever a object found
        if (typeof vl === 'object') {
          ptr1[ky] = resolve(ptr1[ky], vl);
          if(ptr1[ky] !== null) {
            if (ptrs1[dpt+1]) {
              // update if pointer already exists
              ptrs1[dpt+1] = [ptr1[ky], isLast];
            } else {
              // or push a new pointer
              ptrs1.push([ptr1[ky], isLast]);
            }
            ptr1 = ptr1[ky];
          }
        } else {
          ptr1[ky] = vl;
          if (isLast) {
            let ind = dpt;
            let cr = ptrs1[dpt];
            // find the closest sub pointer that should change next
            do {
              ind--;
              cr = ptrs1[ind];
              ptr1 = cr[0];
            } while (cr[1] !== true && ind);
          }
        }
      }
    }, null, obj2);
    return obj1;
  }