【发布时间】:2016-05-26 08:16:59
【问题描述】:
如何将当前对象设置为空?
function ZPNode(){}; //Constructor
function _destroy(){
//this = null; --> Won't work obviously
}
ZPNode.prototype.remove = function(){
//Remove node
_destroy.call(this);
};
编辑:也许描述不清楚。应该发生的事情是这样的:
var tree = ZPTree.create(jsonArr);
var node = tree.findNode(1);
node.remove();
console.log(node); //null
原因很简单。你不想不小心做这样的事情:
node.remove();
node.appendChild(anotherNode); //Oops
如果没有其他方法,可能的解决方案是使用对象的状态。
Edit2:经过更多研究,我认为总体上不可能。我会不情愿地采用解决方法。我可以这样做:
tree.removeNode(node);
虽然在我看来它看起来不那么干净。
【问题讨论】:
-
我觉得你应该看看this answer
-
虽然,它不会删除对象本身。你的函数
remove没有效果 -
你为什么要这样做?
-
@Rayon 因为当用户从树中删除一个节点时,它应该设置为空。我不想强迫他们进入 node=node.remove();
标签: javascript