I am trying to call an IIFE from within a constructor and it doesn't seem to run when I initialize the object. Here's my code:
function TitleView(el) {
this.el = el;
this.parent = el.parentNode;
this.events = {
'onclick' : function() {
console.log('title view onclick');
}
};
(function addEventHandlers() {
for (var ev in this.events) {
if (this.events.hasOwnProperty(ev)) {
this.el[ev] = this.events[ev];
console.log('abc');
}
}
}());
}
var titleView = new TitleView(document.getElementById('task-title-h1'));
My aim here is to attach a bunch of event handlers on object initialization. If I remove the IIFE and keep the body of the code, the event handler is attached to titleView.el, but with the IIFE, it's null. Why is this the case? (This is pure javascript, btw, not a framework.)