3 Replies Latest reply on Oct 5, 2010 10:59 AM by richardh6

    [JS] #targetengine directive and namespaces

    richardh6 Level 1

      I am finally about to set up a menu system for my scripts so that the users can have easier access to them.


      I have just discovered by testing, to my dismay, that if you have one main menu system script which sets up the menu actions and how the events are handled, and that script runs in a persistent engine (for instance, #targetengine "my_menu_system"), then the event handlers which are invoked by the app.doScript command will all run in that same persistent engine, even if the event handlers themselves are separate files without #targetengine directives.


      Which means, of course, that I have to go back through all the scripts and make sure that none of them will be infecting each other's namespaces with identically named global variables and functions.  I generally don't use global variables these days, but some of them were written years ago.  And most of them have a global function called "main()".


      What is the best way to wall them off from each other completely, so I don't have to worry about it?


      So far it seems like the best way is to encapsulate every script in one big anonymous function call, like:



      (function {


      // every piece of code currently in the script


      }) ()


      Does that sound right?