Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Scope of Variables and Functions

You use standard JavaScript syntax to define your extension’s variables and functions. When a Main.html file defines a “global” variable or function, GoLive actually creates the variable or function within the scope of the extension module the file defines, not in the JavaScript global namespace. It is available within the execution scope of that module. It is not available to other extensions.

The only truly global values in the GoLive JavaScript environment are those provided by system-defined global variables, such as the app variable and the document variable. These properties are available to every extension. For a complete listing and description, see Appendix E, “Scoping in JavaScript.”

To demonstrate variable scope in the JavaScript Debugger window, add the following highlighted lines to the Main.html file that you created in Chapter 2, “How to Create an Extension,” and restart GoLive.

// Main.html file for Hello example
<html>
  <body>
    <script>
      var myGlobal = "Hamburg, Liverpool, and London."
   function fabFour()
   {
   writeln ("The Fab Four played in " + myGlobal);
   }
   
   function initializeModule()
   {
   writeln ("Loading the " + module.name + " extension.");
   fabFour();
   }
   
   function terminateModule()
   {
   Window.alert ("Unloading the " + module.name +
   "extension.")
   }
   </script>
   <jsxmodule name="HelloModule" debug>
   </body>
   </html>
   

The new code just adds another writeln statement, but packages it as an extension-specific function. From within the body of this function, the myGlobal variable is accessible as a global variable. However, it is not accessible to any other modules.

The JavaScript Debugger shows the current scope in its module list. To set the execution scope to the HelloModule extension, choose its module name from the pulldown menu, as shown here.

To set the global variable, type the following into the command line:

myGlobal = "Hartford, Hereford, and Hampshire."

Then run the function from the command line:

fabFour();

The function prints the changed string to the output window, using the new value of the global variable.

You might think you can access another module’s variable by evaluating module.var or some similar JavaScript expression, but it is not that simple. You can use the common Object (page 77) to share data among extensions if necessary, although most extensions do not need to do so; see “Sharing Data” on page 37.

Releasing Memory

For optimum performance, you should release unused memory as soon as possible. You can set unneeded JavaScript variables to null to make them available for garbage collection.

Before it unloads your extension, GoLive calls the terminateModule (page 359) function. If you allocate any resources outside the JavaScript environment, your implementation of this function can be used to release them.

  • + Share This
  • 🔖 Save To Your Account