-1

I'm using Javascript within the programming environment of Max/MSP. Here's a basic overview of it's implementation in Max if you're interested. There's nothing particularly unusual there. Just some custom functions/methods available.

So I'm not entirely sure of my terminology here. I have this: var velData = MultiDimensionalArray(8, 16) declared globally, which is referred to in the function below.

Is it a global variable? Since it's calling a function, MultiDimensionalArray, does that make velData a function expression? Either way, I'm unable to access a variable from outside my function:

function list(y) {
    if (inlet == 1) {

        y = arrayfromargs(messagename,arguments);

        for (var i = 0; i < y.length; i++ ) {
            velData[row][i] = y;
        }   
    }

}

post(velData[0][0]);
post();

post() is the equivalent to console.log and post(velData[0][0]) works when it's inside the function but not outside of it. I thought that since velData is declared globally, I should be able to access it outside the function but I can't.

Here is the code on Jsfiddle - http://jsfiddle.net/estevancarlos/WHc5j/

Suggestions?

rpeg
  • 228
  • 3
  • 14
  • Take debugger and go through every line in your code? – zerkms May 19 '14 at 02:47
  • 1
    JavaScript is designed to be "hosted" (as it is by "Max") and as such provides wide latitude to embedders to make the language behave in a particular way. So we (those of us unfamiliar with the Max embedding) can't tell what's happening when you say that velData is "declared globally." What does that mean, in context? Can you provide more detail? – David P. Caldwell May 19 '14 at 02:54
  • 1
    Please show the code where you've declared the variable and assigned the result of the call expression `MultiDimensionalArray(8, 16)`. Also, where is `list()` called? – Bergi May 19 '14 at 02:54
  • 1
    Could you show us where your global variable is in the code? It does not matter if you use a function as long as the function returns some object or value, the variable will be global if defined as so. – Spencer Wieczorek May 19 '14 at 02:55
  • I added a link to my jsfiddle and here it is once more - http://jsfiddle.net/estevancarlos/WHc5j/ – rpeg May 19 '14 at 03:09
  • You don't have a `post` function defined in your demo. – cookie monster May 19 '14 at 03:18
  • Yeah, this might be complicated due to he Max issue. Post works like console.log. It's built in. post(velData[0][0]) works as soon as I put it within the brackets of the function above but maybe that doesn't necessarily answer your question. – rpeg May 19 '14 at 03:20
  • I posed this question with the hopes that it was strictly a JS issue but that may not be immediately evident or necessarily true. Hm :/ – rpeg May 19 '14 at 03:22
  • 1
    What output do you get from post? I think you should get "undefined". – Leo May 19 '14 at 03:26
  • With some further testing, it's possible that the framework i'm using is not properly sending me error messages. I had someone else test it. They received errors. I have not. So I will now investigate that. – rpeg May 19 '14 at 03:29
  • 1
    Let me make a guess. The functions you define is called from hooks in Max/MSP. You get a bit confused by this and tries to call post at the end of the file to get the final values in velData. But the code is not executed in the order you think it is. The code you put in your fiddle just declared there. It is not run there. – Leo May 19 '14 at 03:31
  • 1
    @Leo based on what I'm now hearing elsewhere, that may be it or something like that. Yes. – rpeg May 19 '14 at 03:35
  • 1
    It is a very common mistake the first time you meet hooks in programming... ;-) -- Good luck with this! – Leo May 19 '14 at 03:37

1 Answers1

3

"So I'm not entirely sure of my terminology here. I have this: var velData = MultiDimensionalArray(8, 16) declared globally, which is referred to in the function below. Is it a global variable?"

The velData variable is only declared globally if it is not inside some other function.


"Since it's calling a function, MultiDimensionalArray, does that make velData a function expression?"

No, a function expression has nothing to do with the invocation of a function. It has to do with the manner in which the function is created, which will have no impact on its invocation, aside from a narrow issue or two that has nothing to do with your question.


"Either way, I'm unable to access a variable from outside a loop in my function:"

Then the variable either is not global, or it is being created/initialized sometime after your loop runs.


"post() is the equivalent to console.log and post(velData[0][0]) works when it's inside the function but not outside of it."

Then it would seem that the function it is placed in that causes it to work is being invoked sometime after velData is initialized, whereas if you don't have it inside the function, it's invoked immediately and before velData is initialized.


"I thought that since velData is declared globally, I should be able to access it outside the function but I can't. Suggestions?"

If indeed it's global, then it does sound like a timing issue. You need to track down where and when velData gets its value, and make sure that no other code tries to use velData before that happens.

cookie monster
  • 10,671
  • 4
  • 31
  • 45