【发布时间】:2018-04-19 22:11:03
【问题描述】:
我试图删除一棵树中的一个根节点,而这棵树只有一个节点。由于“this”指的是节点的当前上下文,因此我将其设置为 null 执行 this = null;(下面代码的案例 2)。但是,我收到一条错误消息:ReferenceError: Invalid left-hand side in assignment。一种解决方法是使用delete this.val;(案例1)。
我的问题:为什么我不能设置this = null; 来删除树对象?另一种删除 obj 的方法?谢谢你。
var BST = function(val) {
this.val = val;
this.left = null;
this.right = null;
}
BST.prototype.remove = function(val) {
//delete this.val; // Case 1: OK
this = null; // Case 2: WRONG !
}
BST.prototype.printTree = function() {
if (this === undefined) {
return;
} else
console.log(this.val);
}
var bst = new BST(70);
bst.remove(70);
bst.printTree();
【问题讨论】:
-
你需要实现
BST.removeChild(which)。
标签: javascript