Global variables in .js

Feb 21, 2008 at 8:10pm

Global variables in .js

I am getting into java scripting finally and have a very basic question. My
first project is to cast my random objects into java via mxj. All of my
objects share a single random seed so I am wondering if multiple instances
of “gln.rand.js” can share a variable that holds the global seed.

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

#35980
Feb 21, 2008 at 8:17pm

#123234
Feb 21, 2008 at 8:51pm

Also, from memory, if you declare a top-level variable without “var” in a .js file, it becomes global to all js objects loading that file, ie:

gvar = “check1″; // global to all instances
var avar = “check2″ // local to this instance

#123235
Feb 21, 2008 at 9:13pm

Yes, that seems to be correct although somewhere I think the documentation
says that variable can be know by ALL .js objects in the patch and not just
instances of a particular file. Is this true?

Can you declare a global variable without giving it an initial value or
initialize only when the first instance is loaded?

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

On 2/21/08 3:51 PM, “John Pitcairn” wrote:

>
> Also, from memory, if you declare a top-level variable without “var” in a .js
> file, it becomes global to all js objects loading that file, ie:
>
> gvar = “check1″; // global to all instances
> var avar = “check2″ // local to this instance

#123236
Feb 21, 2008 at 11:27pm

Quote: Gary Lee Nelson wrote on Fri, 22 February 2008 10:13
—————————————————-
> Yes, that seems to be correct although somewhere I think the documentation says that variable can be know by ALL .js objects in the patch and not just instances of a particular file. Is this true?

I think that’s referring to a global Object created with “new Global()”. I remember the global var behaviour I’m referring to being somewhat obscure and undocumented except on this list. I could be wrong on that though.

> Can you declare a global variable without giving it an initial value or initialize only when the first instance is loaded?

Not with the var syntax. But using the object, something like this(?):

var myglobal = new Global(‘globalname’); // access/create a shared named global object, store locally as “myglobal”

if(!myglobal.myvalue && myglobal.myvalue !== 0 && myglobal.myvalue !== false) myglobal.myvalue = “inited”; // test for existence of global object’s “myvalue” property, init if required, note strict type test for false and 0 is required if those might be inited values

#123237
Feb 22, 2008 at 1:04pm

And all is revealed! Thanks EJ.

On 2/21/08 3:17 PM, “Emmanuel Jourdan” wrote:

> Have a look to the Global object (in JavaScriptInMax.pdf).
>
> ej

Cheers
Gary Lee Nelson
Oberlin College
http://www.timara.oberlin.edu/GaryLeeNelson

#123238

You must be logged in to reply to this topic.