Trie实现(Javascript)中存在错误
我正在实现Trie算法的add函数,但它给了我以下错误: 未捕获类型错误:节点不是构造函数 …在此代码行中:Trie实现(Javascript)中存在错误,javascript,tree,trie,Javascript,Tree,Trie,我正在实现Trie算法的add函数,但它给了我以下错误: 未捕获类型错误:节点不是构造函数 …在此代码行中: node.children[char_pos]=new Array(string[0], new node()); 拜托,你能告诉我发生了什么事吗 谢谢你的帮助 这是我的代码: var node = function(){ this.children= new Array(26); this.last_char=false; }; function add(word
node.children[char_pos]=new Array(string[0], new node());
拜托,你能告诉我发生了什么事吗
谢谢你的帮助
这是我的代码:
var node = function(){
this.children= new Array(26);
this.last_char=false;
};
function add(word, node){
char_pos=word.charCodeAt(0) - 'a'.charCodeAt(0);
if(word.length==1){
if(node.children[char_pos]==null){
node.children[char_pos]=word[0];
node.last_char=true;
return;
}else{
return;
};
}else{
if(node.children[char_pos]==null){
node.children[char_pos]=new Array(word[0], new node());
};
};
word=word.substring(1);
add(word, node.children[char_pos][1]);
};
首先你说
var node // = ...
然后
我想您将使用节点的某个实例调用它,一个节点,而不是您的节点构造函数
在函数add中,标识符节点解析为参数“node”,此处不提供具有相同标识符的构造函数。
你会写node=new node()
你会失去node函数吗
Standards通过使用第一个大写字母来命名构造函数或类,例如Node,以及Node的实例Node,从而帮助您完成此任务,这样您就不会覆盖标识符
事实上,您确实应该选择尚未使用的标识符,因为您将覆盖它们,并使后者不可用。请不要为变量使用受保护的名称,例如
string
为什么代码周围有**符号?@Luca Ok。我更改它=)@Geuis,因为我想突出显示给我错误的代码行。
function add(word, node){