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

164
Visualizações
OOP Question About Vanilla JS: The class's constructor won't accept the variable I'm feeding it as a parameter

I'm trying to learn OOP through practice, but I'm pretty stuck at this point. This is the code:

const itemEdit = () => {
  let editIndex = buttonObj.editArr.indexOf(editID);
  console.log(`the editIndex outside of the class is ${editIndex}`);
  if (typeof editIndex != "undefined") {
    editText = new htmlTextualizer(editIndex);
    console.log(
      "new class successfully created as variable is not 'undefined' type"
    );
  }
  editText.printOut();

This is the class/constructor:

class htmlTextualizer {
  constructor(curr) {
    this.curr = curr;
  }

  printOut() {
    console.log(this.curr);
  }
}

The output is either 'undefined' or nothing at all. The logic generally works outside of the function, so I suspect it's something to do with the scope of initiation, but I simply fail to work my way around it. Assistance would be much appreciated. Thanks.

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

0

JavaScript's indexOf() returns -1 if no match is found. That check should look something like this:

if (editIndex > -1) {…}

I'm not sure if that will resolve your problem or not, but it's a problem in general.

Also, if that if statement is not true, and if editText is not defined somewhere outside what you've pasted here, there will be an error because editText is undefined (and doesn't have methods available).

about 4 years ago · Juan Pablo Isaza Relatório

0

There are several things that are unclear about your example, since you reference several undefined objects: buttonObj.editArr, editID, editText.

In general, I would approach testing for existence more carefully. You don't want to attempt to access the indexOf method on something undefined.

I'm not sure what your business logic is exactly, but here is how to do what I think it is: always create the new object, unless buttonObj.editArr contains editID.

Here is how to do that:

const itemEdit = () => {
    if ( !buttonObj || 
         !buttonObj.editArr ||
         (typeof buttonObj.editArr !== "object") ||
         !editID ||
         (buttonObj.editArr.indexOf(editID) < 0) ) {
        editText = new htmlTextualizer(buttonObj.editArr.indexOf(editID));
        console.log("creating instance of class htmlTextualizer");
    }
}
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