I'm currently learning javascript and trying to understand 'this'. In the following code, why am I not able to access this.ul within my render method? (It says it is undefined). I was led to believe that the cacheDOM method would bind this.ul to the people object which could then be accessed by the rest of the Object.
(function(){
var people = {
people: ['Tom', 'Sean'],
init: function() {
this.cacheDOM();
this.render();
},
cacheDOM: function() {
this.input = window.document.querySelector('.input');
this.button = window.document.querySelector('.button');
this.ul = window.document.querySelector('.ul');
},
render: function() {
var data = this.people;
data.map(function(person){
var li = document.createElement('li');
li.textContent = person;
this.ul.appendChild(li);
});
}
};
people.init();
})();
Fixed. Added var ul = this.ul
into the top of my render function which then allowed the map function proper access!