I have a function like this:
const doIt = (name => {
const aLocalVarible = {};
const obj = {
one: function(name) {
Console.log(name);
//Says that the name variable has a value
window[name]();
//Some code
},
two: function() {
//Some code
}
};
return obj;
})();
As you can see, It's a self invoking function with closures. To define aLocalVarible
only for its scope. Everything is fine. but that line window[name]();
which is used to call a variable function doesn't work.
When I run:
doIt.one('getData');
I get this:
TypeError: window[name] is not a function
So why can't it access the global scope? While the function with passed name exists.
EDIT: For more clarification, it is the function which its name is passed. It is in global scope.
const getData = () => {
alert('pagination!');
};
you could try var getData =...
and if this script is global and not type="module" it should place getData on the window. alternatively you can explicitly put it on the window like this: window.getData =...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.