Answer the question
In order to leave comments, you need to log in
Why can't the functions (methods) of a module (Module design pattern) be used in event handlers?
There is the following, very simple, at first glance, code:
"use strict";
let TaskModule = (function () {
return {
msg: function () {
return "Hello World!";
},
showMsg: function () {
alert(this.msg());
},
doTask: function () {
this.showMsg(); //Строка №11
}
}
})();
(function() {
let addButtonElement = document.getElementById("add-task-btn");
addButtonElement.addEventListener("click", TaskModule.doTask, true);
})();
Uncaught TypeError: this.showMsg is not a function
at HTMLInputElement.doTask (main.js:11)
"use strict";
let TaskModule = (function () {
return {
msg: function () {
return "Hello World!";
},
showMsg: function () {
alert(this.msg());
},
doTask: function () {
this.showMsg();
}
}
})();
(function() {
let addButtonElement = document.getElementById("add-task-btn");
let doTaskFn = function() {
TaskModule.doTask();
}
addButtonElement.addEventListener("click", doTaskFn, true);
})();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question