对于JS中二叉搜索树的这部分实现,引用" this._root&#34 ;?的重要性是什么? (为什么他们不能说" this.root")?有关此链接,请访问http://www.nczonline.net/blog/2009/06/16/computer-science-in-javascript-binary-search-tree-part-2/
BinarySearchTree.prototype = {
//more code here
remove: function(value){
var found = false,
parent = null,
current = this._root,
childCount,
replacement,
replacementParent;
//make sure there's a node to search
while(!found && current){
//if the value is less than the current node's, go left
if (value < current.value){
parent = current;
current = current.left;
//if the value is greater than the current node's, go right
} else if (value > current.value){
parent = current;
current = current.right;
//values are equal, found it!
} else {
found = true;
}
}
//only proceed if the node was found
if (found){
//continue
}
},
//more code here
};
答案 0 :(得分:0)
他们可能试图表明不应该在对象外部访问此变量(private
在其他语言中)。
Python有一个强大的惯例,即使用以下划线字符开头的名称来表示此字段或方法是私有的。作者可能正在尝试将相同的约定应用于javascript。
答案 1 :(得分:0)
关于this._root
- 我认为这只是作者的决定,没有任何特殊意义。