2

I'm very new to JavaScript, but this topic seems to have attracted only scant forum attention. Given a number of simple functions:

function do_something(){...};
function do_somemore(){...};
function do_something_else(){...};

I was expecting to be able to assign these explicitly to cells in an (here 2D) array.

myMatrix[5][3] = do_something();
myMatrix[5][4] = do_somemore();
myMatrix[5][5] = do_something_else();

The reason I want to use such an approach are :

  1. simple to understand and maintain.
  2. eliminates potentially redundant anonymous function assignments in the array.
  3. any given function can be assigned to multiple array cells, for example:

    myMatrix[2][6] = do_somemore();
    myMatrix[5][4] = do_somemore();
    myMatrix[6][3] = do_somemore();
    

Unfortunately, calls such as the following (based on various forum examples, plus a little "suck it and see") are all failing.

x = myMatrix[5][4]do_somemore();         -> "missing ; before statement"
x = (myMatrix[5][4])do_somemore();       -> "missing ; before statement"
x = (myMatrix[5][4]do_somemore)();       -> "missing ) in parenthetical"
x = (myMatrix[5][4])(do_somemore());     -> "is not a function"
x = (myMatrix[5][4])()do_somemore();     -> "missing ; before statement"
x = myMatrix[5][4]()do_somemore();       -> "missing ; before statement"
x = myMatrix[5][4]();                    -> "is not a function"
x = (myMatrix[5][4])();                  -> "is not a function"

As I have no knowledge of JavaScript internals, I'd be glad of suggestions how to get the function calls firing.

2
  • Many thanks to everyone who contributed insights. The code was working correctly within a couple of minutes assigning as follows: myMatrix[5][4] = do_somemore; ..and then calling using myMatrix[left][right](); The former I'd already tried, but the latter had escaped me :-)
    – Thug
    Apr 18, 2011 at 15:48
  • Please consider selecting an answer or add more specific information if none of the answers satisfy your question. Jul 10, 2013 at 11:04

4 Answers 4

2

You should assign them like this:

myMatrix[5][3] = do_something;
2
  • 1
    Indeed. At the moment, the OP is calling the functions and assigning the returned values to the matrix. Apr 18, 2011 at 9:14
  • 1
    And the correct syntax to call such a function is x = myMatrix[5][3]();. Also notice that those parentheses are called ‘function call operator’. Apr 18, 2011 at 9:20
0
myMatrix[5][3] = do_something;

Your way would set the value to the RESULT of the function!

0

I'm not entirely clear about what you are after, but:

First, before you can assign a value to an array, that array has to exist:

var myMatrix = [];
myMatrix[5] = [];
myMatrix[5][3] = … // Then you can assign something

Then, if you want to assign the return value of a function:

myMatrix[5][3] = do_something();

Or, if you want to assign the function itself:

myMatrix[5][3] = do_something;

… and then call it and assign its return value to x:

var x = myMatrix[5][3](); 

… which is the same as var x = do_something() except that inside the function this will be myMatrix[5] instead of window.

0
myMatrix[5][3] = do_something; 
myMatrix[5][4] = do_somemore; 
myMatrix[5][5] = do_something_else; 


var x = myMatrix[5][3]();
var y = myMatrix[5][4]();
var z = myMatrix[5][5]();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.