0

我在取消引用 Javascript 对象并将其设置为 NULL 时遇到问题。

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

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?
      }
   }
}
4

3 回答 3

4

请注意,您没有您怀疑的那么大的问题,因为除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];
  }
}

应该足够了。

于 2009-11-20T00:17:05.947 回答
2

一切都是 javascript 是按值传递的,所以“*cur=NULL”是不可能的。您在这里基本上有以下选项

  • 按照您的建议使用 parentID
  • 如果您的文件夹层次结构具有众所周知的根,请从该根浏览以查找父对象
  • 使用类似 DOM removeChild (在父节点上调用),而不是 removeNode (在节点本身上调用)。
于 2009-11-19T23:25:09.020 回答
1

我今天试图做同样的事情。我通过将对象的索引存储为对象本身的属性来解决它。

添加时:

myObj.ID = myArr.push(myObj);

所以要删除它你

myArr[myObj.ID] = null;

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

于 2010-06-02T09:54:01.960 回答