Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!