Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda