Global variables in .js


    Feb 21 2008 | 8:10 pm
    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 www.timara.oberlin.edu/GaryLeeNelson

    • Feb 21 2008 | 8:17 pm
    • Feb 21 2008 | 8:51 pm
      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
    • Feb 21 2008 | 9:13 pm
      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 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
    • Feb 21 2008 | 11:27 pm
      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
    • Feb 22 2008 | 1:04 pm
      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 www.timara.oberlin.edu/GaryLeeNelson