[Home]Javascript/ScopedActions

UseModWiki | Javascript | RecentChanges | Preferences

Scoped actions

Logging

C++

 void f()
     {
     log_block _1("Entering block", "Leaving block" );
     ...
     log( "doing something );
     ...
     if ( ... ) return 1;

     return 2;
     }
produces
 Entering block
 doing something
 leaving block
Javascript equivalent
    function f()
        {
        logging.log_block( "Entering block", "Leaving block", function()
            {
            ...
            logging.log( "doing something" );
            ...
            if ( ... ) return 1;

            return 2;
            } );
        }
where logging.log_block is something like:
   module( "logging.log_block", function() {
       function log_block( enter_message, leave_message, func )
           {
           logging.log( enter_message )
           try {
               func();
               }
           finally
               {
               logging.log( leave_message )
               }
           }   
   return { log_block: log_block };
   } );

Cleaning

 void f()
     {
     f = open( ... );
     janitor( f, close_file );
     ...
     f1 = open( ... );
     janitor( f1, close_file );
     ...
     f2 = open( ... );
     janitor( f2, close_file );
     ...
     }
as opposite to Java, where
 public void f()
     {
     try {
         f = open( ... );
         ...
         try {
             f1 = open( ... );
             ...
             try {
                 f2 = open( ... );
                 ...
                 }         
             finally
                 {
                 f2.close();
                 }
             }
         finally
             {
             f1.close();
             }
        }
    finally
        {
        f.close()
        }
    }
finally
    {
    f.close()
    }
}

To Do - Javascript version


UseModWiki | Javascript | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited February 23, 2003 9:00 am by Mbergal (diff)
Search: