Soy nuevo en Javascript, por lo que solo estoy tratando de implementar una cola básica, pero aparece el error: Error de tipo no detectado: Constructor ilegal. Realmente no sé lo que está mal. Aquí está mi código.
class node{ constructor(id,value){ this.id = id; this.value = value; this.next = null; } getId(){ return this.id; } getValue(){ return this.value; } setValue(value){ this.value = value; } getNext(){ return this.next; } setNext(next){ this.next = next; } } class queue{ constructor(){ this.head = null; this.size = 0; this.tail = null; } enqueue(value){ let newNode = new Node(this.size,value); //This is line 39 if (this.size === 0){ this.head = newNode; this.tail = newNode; this.size++; return; } this.tail.next=newNode; this.tail = newNode; this.size +=1; } dequeue(){ if (this.size > 0){ let temp = this.head.getValue(); this.head = this.head.getNext(); this.size --; return temp; } return null; } } var q= new queue(); q.enqueue(1) console.log(q.dequeue);Lo tira en la línea en cola donde hago un nuevo nodo. ¿Hay alguna limitación para hacer objetos en objetos que no conozco? Adjunto el mensaje de error. Línea 39 en donde creo un nuevo objeto de nodo. Mensaje de error
No, no hay ninguna limitación aquí. Cambiar Nodo a nodo. La carcasa debe coincidir. Sugiero formatear el código correctamente y buscar algunas de las convenciones JS (las clases deben ser PascalCase, o al menos seguir la misma convención). Tampoco use var, es una función obsoleta con problemas de alcance, especialmente para los novatos.
El problema fue con el error tipográfico del nombre de clase para el nodo en function enqueue(value).
Aquí está el código de trabajo después de actualizar el mismo-
enqueue(value){ let newNode = new node(this.size,value); //This is line 39 if (this.size === 0){ this.head = newNode; this.tail = newNode; this.size++; return; } this.tail.next=newNode; this.tail = newNode; this.size +=1; }