I have a decent feel for scope and execution flow, but I'm having trouble grasping why this doesn't work:
var modalWindow = (function() {
// Other code...
modalBtn.addEventListener('click', modalWindow.closeModal);
return {
closeModal: function() {
modalContainer.remove();
}
}
}());
And this does:
var modalWindow = (function() {
// Other code...
modalBtn.addEventListener('click', function() {
modalWindow.closeModel());
});
return {
closeModal: function() {
modalContainer.remove();
}
}
}());
The first throws modalWindow
is undefined. I know I could just declare a named object and place closeModal
in it, then reference it, and I wouldn't need the anonymous function in the listener. But I'm curious as to why the latter works as is.