有没有一种方法可以删除 Ace 中的循环引用

Is there a method to remove circular references in Ace

我在 Web 视图中包装 AceEditor 页面并将事件结果返回到容器,它挂在包含循环引用的对象上。

ace中是否有方法可以删除 Ace Editor 事件中调度的对象中的循环引用?

没找到所以自己写了一个。到目前为止似乎有效:

"use strict";

// returns an object that removes circular references
document.removeReferences = function (object, maxDepth, depth, objects, clone, debug) {
  if (objects==null) { objects = []; }
  if (clone==true) { clone = {}; }
  maxDepth = maxDepth==null ? 10 : maxDepth;
  depth = depth==null ? 0 : depth;
  var padding = "";
  var value;

  if (debug) { for (var i=0;i<depth;i++) { padding += "  "; } };

  if (debug && typeof object != 'function') {
    console.log(padding + "\nDepth:" + depth + " max depth:" + maxDepth);
  }

  if (typeof object == 'object') {
    if (object!=null && objects.indexOf(object)==-1) {
      if (debug) console.log(padding+"caching object:"+object);
      objects.push(object);
    }
    else {
      if (debug) console.log(padding+"object found. skipping");
      return clone;
    }

    for (var key in object) {
      value = object[key];

      if (debug && typeof value!="function") {
        console.log(padding+""+key + ":" + typeof value);
      }

      if (object.hasOwnProperty(key) && (typeof value == 'object')) {

        // remove circular references
        if (objects.indexOf(value)!=-1) {
          if (debug) console.log(padding+"recursive object found. deleting:"+key);

          if (clone==null) {
            delete object[key];
          }
          else {
            delete clone[key];
          }

          continue;
        }

        if (depth>=maxDepth) {
          //object[key] = null;

          if (clone==null) {
            if (debug) console.log(padding+"max object limit - deleting:"+key);
            delete object[key];
          }
          else {

            if (typeof value=="array") {
              clone[key] = null;
            }
            else {
              clone[key] = null;
            }
            delete clone[key];
            if (debug) console.log(padding+"max object limit - not adding:"+key); 
          }

        }
        else {

          if (clone==null) {
            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, null, debug);
            }
          }
          else {
            if (typeof value=="array") {
              clone[key] = value.slice();
            }
            else {
              clone[key] = value!=null ? {} : null;
            }

            if (value!=null) {
              if (debug) console.log(padding+"diving into:"+key);
              document.removeReferences(value, maxDepth, depth+1, objects, clone[key], debug);
            }
          }
        }
      }
      else if (typeof value == 'function') {
          //object[key] = null;
          if (clone==null) {
            delete object[key];
          }
          else {

          }
      }
      else {

        if (clone!=null) {
          if (debug) console.log(padding+" cloning key :"+key);

          if (typeof value=="array") {
            clone[key] = value.slice();
          }
          else {
            clone[key] = value!=null ? value : null;
          }
        }
      }
    }
  }

  if (clone!=null) {
    if (debug) console.log(padding+"return clone");
    return clone;
  }

  if (debug) console.log(padding+"return object");
  return object;
}

然后你使用这样的东西:

// inside an ace editor event: 
var debug = true;
var clonedEvent = document.removeReferences(event, 0, null, null, true, debug);