1

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
        }
    }    
}​
4
  • 3
    I believe this previous SO answer handles your case. Basically it comes back to scope. Commented Dec 18, 2012 at 3:50
  • @JacquesChester It appears to be addressing a slightly different problem - I'm specifically trying to call a function from a specific index of an array. Commented Dec 18, 2012 at 4:04
  • I think the biggest issue is that you're trying to call a native function from the specific index of an array. The invocation getOutput([function(msg){alert(msg);}], ["Hi!", "Hello!", "Lolwut?"]) works. Commented Dec 18, 2012 at 4:09
  • 1
    @AndersonGreen If you look at the accepted answer, it's the same basic logic as what I linked to. I'm happy to give matehat the credit on this one, he bothered to actually write an answer :D Commented Dec 18, 2012 at 4:23

4 Answers 4

3

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 :

function F(arg) {alert(arg);}

in place of alert in 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 :

getOutput([alert], ["Hi!", "Hello!", "Lolwut?"]);

function getOutput(functions, arguments){
    for(var i = 0; i < functions.length; i++){
        for(var j = 0; j < arguments.length; j++){
            var f = functions[i];
            f(arguments[j]);
        }
    }    
}​
Sign up to request clarification or add additional context in comments.

4 Comments

Placing a parenthesis around it might work too (functions[i])(arguments[i]);
I just tried and unfortunately it doesn't (that's what I was going to post first). ([alert][0])("hello!"); raises the same error.
Maybe ([alert][0]).call(null, arguments[j])? (just for the sake of trying alternatives haha)
It does! But I guess the property access construct pretty much had to work. Otherwise it'd be breaking quite a lot of sites :)
1

do you mean something like:

function getOutput(fn, args) { 
 for(var f = 0; f < fn.length; f++){
    for(var j = 0; j < args.length; j++){
        fun = fn[f];
        fun.call(this, args[j]);
    }
  }

}
getOutput([alert], ["Hi!", "Hello!", "Lolwut?"]);

Comments

1

i believe it is because you're calling an object outside of its context the method.call(args) described here http://www.devguru.com/technologies/ecmascript/quickref/call.html should allow for it.

functions[i].call(this, arguments[j])

Comments

0

i belive it would be like this:

fun1(fun2('lol'));

or the hard way

var hi7 = fun2('lol')

fun1(hi7)

4 Comments

I don't see how this is relevant - can you explain further?
The question is asking how to use the output of a function of in a functions parameters
No, the question is asking about using pointers to the actual functions as parameters.
Oh sorry, very little JS Knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.