I have the following JavaScript class:
class TrieNode
{
constructor()
{
switch(arguments.length)
{
case 0 : this.constructorNoParam();
break;
case 1 : this.constructorOneParam(arguments[0]);
break;
}
}
constructorOneParam(c)
{
this.children=new TrieNode();
this.c = c;
this.isLeaf;
}
constructorNoParam()
{
this.children = new TrieNode();
this.c;
this.isLeaf;
}
}
The reason why I am getting this error is that every time I am creating the children
variable, the constructor creates another instance of the TrieNode class and results in an infinite loop.
Is there a way that I can create only ONE variable for the whole class? I had to put it in the constructor since in JavaScript classes, variables can only be created inside functions.
Basically, what I want to achieve would look like this in java:
public class TrieNode {
public char c;
TrieNode children = new TrieNode();
public boolean isLeaf;
public TrieNode() {}
public TrieNode(char c){
this.c = c;
}
Thanks