0

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);
        }
    }
};
RVos86
  • 1

0 Answers0