Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Illegal constructor Java script

I am new to Javascript and so I am just trying to implement a basic queue, but I get the error: Uncaught TypeError: Illegal Constructor. I really don't know what is wrong. Here is my code.

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);

It throws it on the line in enqueue where I make a new node. Is there some limitation to making objects in objects that I don't know? I have attached the error message. Line 39 in the where I make a new node object.Error Message

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

No there aren't any limitations here. Change Node to node. The casing should match. I suggest formatting the code correctly and looking up some of the JS conventions (classes should be PascalCase, or at least follow the same convention). Also don't use var, it's an outdated feature with scoping issues especially for newbies.

about 4 years ago · Juan Pablo Isaza Relatório

0

The issue was with the typo of class name for node in function enqueue(value).

Here is the working code after updating the same-

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;
  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda