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 fuction calls firing.

share|improve this question
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 '11 at 15:48
feedback

5 Answers

You should assign them like this:

myMatrix[5][3] = do_something;
share|improve this answer
1  
Indeed. At the moment, the OP is calling the functions and assigning the returned values to the matrix. – Marcel Korpel Apr 18 '11 at 9:14
And the correct syntax to call such a function is x = myMatrix[5][3]();. Also notice that those parentheses are called ‘function call operator’. – Marcel Korpel Apr 18 '11 at 9:20
feedback
myMatrix[5][3] = do_something;

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

share|improve this answer
feedback

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.

share|improve this answer
feedback
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]();
share|improve this answer
feedback

You are executing the functions immediately, so the value of e.g. myMatrix[5][5] will be the return value of that function. If you want myMatrix[5][5] to be a pointer to the function (and so be able to execute it later), use myMatrix[5][5] = do_something_else.

example:

var somearray = [[null,null]];
function test(){
  return 'tested';
}
somearray[0][0] = test;
alert(somearray[0][0]()); //=> 'tested'
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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