variable scope across script instances

Jun 14, 2006 at 8:36am

variable scope across script instances

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

#26406
Jun 16, 2006 at 5:57am

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;

#78885
Jun 16, 2006 at 8:16am

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

#78886
Jun 16, 2006 at 4:20pm

great – thank you!

#78887
Jun 19, 2006 at 12:40am

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…

#78888
Jun 19, 2006 at 9:38am

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

#78889

You must be logged in to reply to this topic.