Forums > Javascript

variable scope across script instances

June 14, 2006 | 8:36 am

I’ve created a javascript that draws a trace across the screen. I am trying to instantiate multiple versions of my patch that contains the javascript itself. however, when I do this, I don’t see multiple objects, rather, it appears that the last instantiation overrides the previous…I can’t seem to keep them separate.

I am suspecting that this is due to the scope of my variables in the javascript. my first question is this:

if global javascript variables are defined, will there be collision of multiple instances of that script are called? or is the scope restricted to the one patcher?

second question:

what is the scope of ‘attributes’ defined with the declareattribute() function? are these also globals? or are they more protected than that?

thanks!
david


June 16, 2006 | 5:57 am

If you declare the variable with "var" it’s local to that instance of the js. If you don’t, it’s global to all instances of js object (regardless of the js file used), ie:

var unique = 1;
shared = 2;


June 16, 2006 | 8:16 am

Yeah, and it’s an "artifact" of javascript. If you need to have
shared variable, I would recommend using the Global object (p. 29, in
the javascriptInMax.pdf).

Best,
ej


June 16, 2006 | 4:20 pm

great – thank you!


June 19, 2006 | 12:40 am

Quote: Emmanuel Jourdan wrote on Fri, 16 June 2006 20:16
—————————————————-
> Yeah, and it’s an "artifact" of javascript.

Do you mean it’s considered a bug and may change?

Not that I’m ever using the non-var global, I always use the global object, but others may be making use of it…


June 19, 2006 | 9:38 am

On 19 juin 06, at 02:40, John Pitcairn wrote:

> —————————————————-
>> Yeah, and it’s an "artifact" of javascript.
>
> Do you mean it’s considered a bug and may change?

I think it’s something which is part of JavaScript.

> Not that I’m ever using the non-var global, I always use the global
> object, but others may be making use of it…

I remember Joshua said something about that… He might confirm or
not…

ej


Viewing 6 posts - 1 through 6 (of 6 total)