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

190
Views
JavaScript: this keyword not working inside of object?

I have this object "names" which holds an array of names, a function to add a name to the array and to make an unordered list of the array. I would expect that "this.names" would refer to the array in question but I keep getting "undefined". When I use "names.names" it works perfectly but inside of the names object, the "this" keyword should work, right? What am I doing/seeing wrong here?

const names = {
    names: ["Kevin", "Joey", "Achmed", "Bob", "Leo", "Hank"],
    addName: name => this.names.push(name),
    toList: listId => {
        this.names.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
        let ul = document.createElement("ul");
        for (let i of this.names) {
            let li = document.createElement("li");
            li.innerHTML = i;
            ul.appendChild(li);
            document.getElementById(listId).appendChild(ul);
        }
    }
};
about 4 years ago · Juan Pablo Isaza
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!