Advertisement
RokiAdhytama

Scoping - Chapter 10

Jun 26th, 2022
976
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.61 KB | None | 0 0
  1. <html xmlns = "http://www.w3.org/1999/xhtml">
  2.    <head>
  3.       <title>A Scoping Example</title>
  4.  
  5.       <script type = "text/javascript">
  6.          <!--
  7.         var x = 1;      // global variable
  8.  
  9.         function start()
  10.         {
  11.            var x = 5;   // variable local to function start
  12.  
  13.            document.writeln( "local x in start is " + x );
  14.  
  15.            functionA(); // functionA has local x
  16.            functionB(); // functionB uses global variable x
  17.            functionA(); // functionA reinitializes local x
  18.            functionB(); // global variable x retains its value
  19.  
  20.            document.writeln(
  21.               "<p>local x in start is " + x + "</p>" );
  22.         }
  23.  
  24.         function functionA()
  25.         {
  26.            var x = 25;  // initialized each time
  27.                         // functionA is called
  28.  
  29.            document.writeln( "<p>local x in functionA is " +
  30.                              x + " after entering functionA" );
  31.            ++x;
  32.            document.writeln( "<br />local x in functionA is "
  33.                              + x + " before exiting functionA"
  34.                              + "</p>" );
  35.         }
  36.  
  37.         function functionB()
  38.         {
  39.            document.writeln( "<p>global variable x is " + x +
  40.                              " on entering functionB" );
  41.            x *= 10;
  42.            document.writeln( "<br />global variable x is "
  43.                              + x + " on exiting functionB"
  44.                              + "</p>" );
  45.         }
  46.         // -->
  47.       </script>
  48.  
  49.    </head>
  50.    <body onload = "start()"></body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement