Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1589 Does JavaScript support inner functions, in the same way that Java supports inner methods?

irt.org | Knowledge Base | JavaScript | Object | Q1589 [ previous next ]

Q1589 Does JavaScript support inner functions, in the same way that Java supports inner methods?

Yes. I believe JavaScript 1.2 supports this:

<script language="JavaScript1.2"><!--

function outer() {
   document.write('starting outer()<br>');

   function inner() {
     document.write('starting outer().inner()<br>');
     document.write('ending outer().inner()<br>');
   }
   
   inner();
   document.write('ending outer()<br>');
}

// just to confuse things we'll define an inner() function
// all on its own:

function inner() {
   document.write('starting inner()<br>');
   document.write('ending inner()<br>');

   function nested() {
     document.write('a nested funtion');
   }
}

// invoke the outer() function, which in turn should invoke
// its own inner function:

outer();

// invoke the inner() function. Should invoke the non nested
// inner() function:

inner();

// attempt to invoke the nested() function, nested within
// the inner() function. Fails with "nested is not defined."

nested();
//--></script>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.