I'm struggling to call a Javascript function dynamically in my code.
const MyObject = (function() {
function _myPrivateFunction() {
console.log('_myPrivateFunction()');
}
const _MyObject = function() {
console.log('_MyObject');
};
_MyObject.prototype = {
myPublicFunction: function() {
_myPrivateFunction(); // Ok
let functionName = '_myPrivateFunction';
window[functionName](); // Uncaught TypeError: window.functionName is not a function
window["functionName"](); // Same error.
}
};
return {
init: _MyObject
}
})();
let obj = new MyObject.init();
obj.myPublicFunction();
Can someone show me the correct way to achieve this ?