简体   繁体   English

传递默认值时,如何解决“节点未定义”错误?

[英]How to fix 'node is undefined' error when a default value is being passed?

I am trying to implement Trie data structure in javascript. 我正在尝试在javascript中实现Trie数据结构。 Inside the print function which just prints an array containing all the word in the trie, I have another function search, which searches for all words in the trie and pushes them to the array. 在仅打印包含trie中所有单词的数组的print函数内部,我还有另一个函数搜索,该函数搜索trie中的所有单词并将其推入数组。 search function two parameters 'node' and 'str' which both have default values. 搜索功能两个参数'node'和'str'均具有默认值。 When I invoke the function without passing any argument the error occurs although the node has a default value and is not undefined. 当我在不传递任何参数的情况下调用该函数时,尽管该节点具有默认值且未定义,但仍会发生错误。

this.print = function(){
        let words = new Array();
        let search = function(node = this.root, string = new String()){
            if(node.keys.size != 0){
                if(node.isEnd() === true){
                    words.push(string);
                }
                for (let letter of node.keys.keys()){
                    search(node.keys.get(letter), string.concat(letter));
                };
            }else{
                (string.length > 0) ? words.push(string) : undefined;
                return;
            }
        };
        search();
        return words.length > 0 ? words : null;
    };

TypeError: node is undefined trie.js:44:16 TypeError:节点未定义trie.js:44:16

search http://127.0.0.1:5500/trie.js:44
print http://127.0.0.1:5500/trie.js:56
<anonymous> http://127.0.0.1:5500/trie.js:

Try this: 尝试这个:

this.print = function(self=this){
    let words = new Array();
    let search = function(node = self.root, string = new String()){
        if(node.keys.size != 0){
            if(node.isEnd() === true){
                words.push(string);
            }
            for (let letter of node.keys.keys()){
                search(node.keys.get(letter), string.concat(letter));
            };
        }else{
            (string.length > 0) ? words.push(string) : undefined;
            return;
        }
    };
    search();
    return words.length > 0 ? words : null;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问对象上具有默认值的字段时如何解决流错误? - How to fix Flow error when accessing a field with default value on an object? 在 Angular 中导入 protractor 时如何修复“浏览器”未定义 - How to fix 'browser' being undefined when importing protractor in Angular mysql node.js 查询错误(未定义)? 如何修复它 - mysql node.js query error(undefined)? how to fix it 如何修复节点中的“无法读取未定义的属性&#39;trim&#39;”错误 - How to fix “Cannot read property 'trim' of undefined” error in node 如何修复未定义的错误处理 node.js - How to fix undefined error handling node.js 当 function 循环时,如何修复“函数返回未定义、预期 Promise 或值”错误 - How to fix “Function returned undefined, expected Promise or value” error when function loops 传递给 onChange 方法的子组件时出现“无法读取未定义道具”的错误 - Getting error of "Cannot read props of undefined" when being passed to child component for onChange method 如何解决这个错误? 未定义 - How to fix this error? Undefined 如何修复传递给部分数据的部分作为属性添加的错误 - How to fix error with parts of the data passed through to partial being added as attributes 从json文件导入时,如何修复未定义对象数组的变量? - How can I fix variables of an array of objects being undefined when being imported from a json file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM