3

How can I make this work:

q = {}

q[0] = new Array()

q[0].push(function()
{
   console.log("whatup")
})

q[0]() // currently prints "q[0] is not a function"

Of course, it shouldn't execute until I call it. I tried nesting it inside another function as a return value, but nothing works :(

1
  • 1
    Someone deleted their answer that had helpful information. You're actually creating an object filled with arrays, because you initialize q with {}, to create an array of arrays, use q = [] instead. Commented Apr 15, 2011 at 17:09

3 Answers 3

6

You have a multidimensional array, but you're only accessing the first dimension at the end (which is another array). Try q[0][0]().

Sign up to request clarification or add additional context in comments.

Comments

3

The problem is that the value in q[0] is the new Array() that you assigned to it -- not a function.

These lines:

q[0].push(function()
{
   console.log("whatup")
})

will put the function as the first element of the new Array you assigned to q[0] in the second line. So instead of this:

q[0]() // currently prints "q[0] is not a function"

You're really wanting to do this:

q[0][0]()

Comments

2

Your code should work but you're calling the array. To call the function you need to do

q[0][0]()

Comments

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.