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

71
Views
Todo list using Class

So I am learning JS classes and I am facing already an issue at start, I can't add any todo to my list. I can't find solution what I'm doing wrong. My code:

<input type="text" id="addTodo" />
<button onClick="TodoAddCommand">
Add
</button>

let Todos = [
{
 id: 0,
 content: "test",
 done: false
}

]
class TodoAddCommand {
    constructor(e) {
  this.event = e;
  }
  
  execute() {
  const todo = {
    id: Todos.lenght,
    content: this.event.data,
    done: false,
  }
  
  Todos.push(todo);
  console.logo(Todos);
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

TodoAddCommand is a class; you are trying to call it like a function.

Instead, create a new instance of TodoAddCommand and pass the event to the constructor, then call execute():

let Todos = [{
    id: 0,
    content: "test",
    done: false
  }

]
class TodoAddCommand {
  constructor(e) {
    this.event = e;
  }

  execute() {
    const todo = {
      id: Todos.lenght,
      content: this.event.data,
      done: false,
    }

    Todos.push(todo);
    console.log(Todos);
  }
}
<input type="text" id="addTodo" />
<button onClick="new TodoAddCommand(event).execute()">
Add
</button>

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!