Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Object | Q1589 [ previous next ]

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>

©2018 Martin Webb