Javascript - 从外部数组中删除对我的对象的引用
我在取消引用 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?
}
}
}
I have a problem with dereferencing a Javascript object and setting it to NULL.
Here, I have a Folder implementation that supports recursive subdirectory removal. Please see my comments to understand my dilemma.
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?
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
javascript中的一切都是按值传递的,所以“*cur=NULL”是不可能的。您基本上有以下选项
Everything is javascript is passed by value, so "*cur=NULL" is not possible. You basically have the following options here
今天我也想做同样的事情。
我通过将对象的索引存储为对象本身的属性来解决这个问题。
当你添加它时:
所以要删除它,
我想你现在已经解决了它,但你几乎可以做同样的事情;而且它比使用对象更简单。
I was trying to do the same thing today.
I've worked around it by storing the object's index as a property of the object itself.
When you add it:
So to remove it you
I guess you solved it by now, but you could do almost the same; and it's simpler than using objects.
请注意,您的问题并不像您怀疑的那么大,因为
RemoveDir
中除folder
之外的所有子目录都将从其父目录的subdir
中删除> 通过stack.push(cur.subdirs.pop());
行要在父目录中查找子目录,您可以使用对象作为字典而不是数组
subdirs
:给定一个文件夹,您可以使用以下方法从父文件夹中删除该文件夹:
这是预购版本:
递归后购版本:
迭代后购版本:
不过,除非您需要采取其他步骤处理已删除的文件时&文件夹,一个简单的:
应该足够了。
Note that you don't have as big a problem as you suspect, since all subdirectories but
folder
in yourRemoveDir
will be deleted from their parent'ssubdir
by thestack.push(cur.subdirs.pop());
lineTo find a subdirectory in a parent, you could make use an object-as-dictionary rather than an array for
subdirs
:Given a folder, you can remove the folder from the parent with:
Here's a preorder version:
And the recursive post-order version:
And the iterative post-order version:
Though, unless you need to take additional steps when processing deleted files & folders, a simple:
should suffice.