【发布时间】:2010-12-18 13:03:13
【问题描述】:
我在取消引用 Javascript 对象并将其设置为 NULL 时遇到问题。
在这里,我有一个支持递归子目录删除的文件夹实现。请查看我的 cmets 以了解我的困境。
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?
}
}
}
【问题讨论】:
-
有什么原因你没有对
RemoveDir使用递归?是否有任何示例中未显示的附加处理需要在文件或文件夹被删除时执行? -
@outis:我只是更喜欢迭代而不是递归。在某些情况下,递归执行它可能更节省空间,但我知道宽度(每个级别的子目录数量)相对于深度会很小。实际上,我的应用程序与文件夹和文件无关,但它具有相同的结构。我的“文件夹”对象还有更多属性需要处理和删除。
标签: javascript null dereference