代码之家  ›  专栏  ›  技术社区  ›  Jeff Meatball Yang

javascript-从外部数组中删除对我的对象的引用

  •  0
  • Jeff Meatball Yang  · 技术社区  · 15 年前

    我在取消引用一个javascript对象并将其设置为空时遇到问题。

    这里,我有一个支持递归子目录删除的文件夹实现。请看我的评论来理解我的困境。

    function Folder(name, DOM_rows) {
        this.name = name;
        this.files = [].concat(DOM_rows);
        this.subdirs = [];
    }
    
    Folder.prototype.AddDir(name, DOM_rows) {
       this.subdirs.push(new Folder(name, DOM_rows));
    }
    
    Folder.prototype.RemoveDir(folder) {
       var stack = [folder];
       while(stack.length > 0) {
          var cur = stack.pop();
          // do a post-order depth-first traversal, so dig to the deepest subdir:
          if(cur.subdirs.length > 0) {
              while(cur.subdirs.length > 0) { stack.push(cur.subdirs.pop()); }
          } else {
              // arrived at a leaf-level:
              cur.files = null;
              // now how do I delete cur from it's parent's subdirs array?
              // the only way I know how is to keep a "cur.parentDir" reference,
              // then find parent.subdirs[ index of cur ] and slice it out.
              // How can I do the JS-equivalent of *cur = NULL?
          }
       }
    }
    
    3 回复  |  直到 14 年前
        1
  •  4
  •   outis    14 年前

    注意,你没有你怀疑的那么大的问题,因为所有的子目录 folder 在你 RemoveDir 将从其父级中删除 subdir stack.push(cur.subdirs.pop()); 线

    要在父目录中查找子目录,可以使用对象作为字典而不是数组 subdirs :

    function Folder(name, DOM_rows, parent) {
        this.name = name;
        this.parent = parent;
        this.files = [].concat(DOM_rows);
        this.subdirs = {};
        this.subdirCount = 0;
    }
    
    Folder.prototype.AddDir = function (name, DOM_rows) {
        if (this.subdirs[name]) {
            return null;
        }
        ++this.subdirCount;
        return this.subdirs[name] = new Folder(name, DOM_rows, this);
    }
    

    给定一个文件夹,您可以从父文件夹中删除该文件夹:

    delete folder.parent.subdirs[folder.name];
    

    以下是预购版本:

    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
          var stack = [folder];
          while(stack.length > 0) {
              var cur = stack.pop();
              // pre-order
              delete cur.files;
              // if there's other processing to be done, now's the time to do it
              for (subdir in cur.subdirs) {
                  stack.push(cur.subdirs[subdir]);
                  delete cur.subdirs[subdir];
              }
              // it's unnecessary to set subdir count, since 'cur' has been deleted
              //cur.subdirCount = 0;
          }
          delete this.subdirs[folder.name];
          --this.subdirCount;
      }
    }
    

    递归后序版本:

    Folder.prototype.RemoveChildren = function () {
        for (subdir in this.subdirs) {
            this.RemoveDir(this.subdirs[subdir]);
        }
    }
    
    Folder.prototype.RemoveDir = function (folder) {
        if (this.subdirs[folder.name] === folder) {
            folder.RemoveChildren();
            folder.files = [];
            delete this.subdirs[folder.name];
            --this.subdirCount;
        }
    }
    

    迭代后序版本:

    Array.prototype.top = function () { return this[this.length-1]; }
    
    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
          var stack = [folder];
          while(stack.length > 0) {
              var cur = stack.top();
              if (cur.subdirCount > 0) {
                  for (subdir in cur.subdirs) {
                      stack.push(cur.subdirs[subdir]);
                      delete cur.subdirs[subdir];
                  }
                  cur.subdirCount = 0;
              } else {
                  stack.pop();
                  delete cur.files;
                  // other post-order processing
              }
          }
          delete this.subdirs[folder.name];
      }
    }
    

    但是,除非在处理已删除的文件和文件夹时需要采取其他步骤,否则简单的:

    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
        delete this.subdirs[folder.name];
      }
    }
    

    应该足够了。

        2
  •  1
  •   user187291    15 年前

    一切都是通过值传递的javascript,因此“*cur=null”是不可能的。你基本上有以下选择

    • 按照您的建议使用parentid
    • 如果文件夹层次结构具有已知根,请从该根目录浏览以查找父对象
    • 使用类似于dom removechild(在父节点上调用)的方法,而不是removenode(在节点本身上调用)。
        3
  •  1
  •   DasRakel    14 年前

    我今天也在做同样的事情。 我通过将对象的索引存储为对象本身的属性来解决这个问题。

    添加时:

    myObj.ID = myArr.push(myObj);
    

    所以把它移走你

    myArr[myObj.ID] = null;
    

    我想你现在已经解决了,但是你可以做几乎相同的事情;而且它比使用对象简单。