Whenever I try to run this function in Chromium, I get the error message “Uncaught TypeError: Illegal invocation”. Why does this occur, and how can I resolve it?
getOutput([alert], ["Hi!", "Hello!", "Lolwut?"]); //why doesn't this call "alert"
//for each of the arguments?
//this function is supposed to return the output of each function for each argument.
function getOutput(functions, arguments){
for(var i = 0; i < functions.length; i++){
for(var j = 0; j < arguments.length; j++){
functions[i](arguments[j]); //why doesn't this call the function
}
}
}
EDIT:
Though the fix given here works, the reason seems to be as rbtLong suggested, that the native function (specifically here
alert) is called outside of its context. Using a wrapper like this :in place of
alertin your code as is makes the code run. Still, the suggested fix below works if you want to have a general purpose function that can take native function.(BTW: The same occurs in Safari and Firefox)
It seems to have something to do with the array access construct not allowing invocation right after it. Maybe much like you can’t do
1.toString(). You could quickly fix it like this :