Scope Rules (WML)
Scope Rules The scope of a variable is the portion of the program in which the identifier can be referenced. A local variable declared in a WMLScript function can be used only in that function. All WMLScript variables are local. Browser variables (similar to what other languages call global variables) are global to a WML document. However, in a WMLScript document they must be accessed via methods of the WMLBrowser object. Identifiers declared inside a function have function (or local) scope. Function scope begins with the opening left brace ({) of the function in which the identifier is declared and ends at the terminating right brace (}) of the function. Local variables of a function have function scope; so do function parameters, which are also local variables of the function. A local variable is allowed to have the same identifier as a browser variable, since browser variables can only be manipulated using setVar and getVar. The script of Fig. 16.8 demonstrates scoping issues in WMLScript with browser variables and local variables. This example illustrates that local variables and browser variables can have the same name, and will not effect each other. It also shows that functions can read and write the same browser variables, but cannot alter other functions’ local variables. 1 // Fig 16.8: scoping.wmls 2 // Variable scoping in WMLScript 34 extern function start() 5 { 6 WMLBrowser.setVar( "x", "1" ); // Browser variable 7 var x = 5; // variable local to function start 89 WMLBrowser.setVar( "display", "local x in start is " 10 + x + "\n" ); 11 WMLBrowser.refresh(); 12 Fig. 16.8 Variable scoping in WMLScript. 300 WMLScript: Functions Chapter 16 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. The start function begins on line 6 by initializing the browser variable x and setting it to 1. The browser variable x can now be read and set from all cards and functions. Next, on line 7, a local variable x is declared and set to 5. Although both variables have the name, 13 functionA(); // has local x 14 functionB(); // uses browser variable x 15 functionA(); // reinitializes local x 16 functionB(); // browser variable x retains its value 17 18 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 19 + "\nlocal x in start is " + x ); 20 WMLBrowser.refresh(); 21 } 22 23 extern function functionA() 24 { 25 var x = 25; // initialized each time functionA is called 26 27 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 28 + "\nlocal x in functionA is " + x + 29 " after entering functionA" ); 30 WMLBrowser.refresh(); 31 32 ++x; 33 34 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 35 + "\nlocal x in functionA is " + x + 36 " before exiting functionA\n" ); 37 WMLBrowser.refresh(); 38 } 39 40 extern function functionB() 41 { 42 var changeX = WMLBrowser.getVar( "x" ); 43 44 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 45 + "\nbrowser variable x is " 46 + WMLBrowser.getVar( "x" ) + 47 " on entering functionB" ); 48 WMLBrowser.refresh(); 49 50 changeX *= 10; 51 52 WMLBrowser.setVar( "x", changeX ); 53 WMLBrowser.refresh(); 54 55 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) 56 + "\nbrowser variable x is " 57 + WMLBrowser.getVar( "x" ) + 58 " on exiting functionB\n" ); 59 WMLBrowser.refresh(); 60 } Fig. 16.8 Variable scoping in WMLScript. Chapter 16 WMLScript: Functions 301 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. they are not related. Altering one will not effect the other in any way. Two separate variables now exist—a browser variable x that has the value 1 and a local variable x with a value of 5. Lines 9–11 WMLBrowser.setVar( "display", "local x in start is " + x + "\n" ); WMLBrowser.refresh(); create a message that shows the value of the local variable x by concatenating strings together with the value and using the setVar method to update the variable display. The refresh method is used to update the value of the variable in the browser. Later, we reference the value of display to show the message to the user. Lines 13–16 functionA(); // has local x functionB(); // uses browser variable x functionA(); // reinitializes local x functionB(); // browser variable x retains its value call the functions functionA and functionB twice each. These functions manipulate the browser variables and their own local variables and print the values. Lines 18 through 20 print the value of the local variable x to prove that it has not been changed in either of the functions that were called. The value of the local variable x is not changed within the start function before displaying it again. It’s value continues to be 5. Next, another local variable x is created in functionA on line 25. Again, the value of x is relevant only within functionA. It cannot be altered by any other function, nor does it affect the value of the local variable x in function start. This time, x is set to 25. In lines 27–30, we concatenate the value of the local variable x and a short description onto the value of variable display and update the variable using the refresh method. In line 32 the value of the local variable x increments using the ++ operator. The local variable x is now 26. Next, the variable’s value and a short description are again concatenated onto the value display on lines 34–37. The next function, functionB, begins by retrieving the value of the browser variable x and storing it in a new local variable changeX. Later, the value of changeX is altered and then stored back into the browser variable x. Lines 44–48 WMLBrowser.setVar( "display", WMLBrowser.getVar( "display" ) + "\nbrowser variable x is " + WMLBrowser.getVar( "x" ) + " on entering functionB" ); WMLBrowser.refresh(); concatenate a phrase including the current value of the browser variable x onto the variable display. Method getVar retrieves the value of the browser variable. Remember, the browser variable x was initialized to 1 by the start function. It has not been altered since, so it still holds that value. Lines 50–53 changeX *= 10; WMLBrowser.setVar( "x", changeX ); WMLBrowser.refresh(); 302 WMLScript: Functions Chapter 16 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. update the value of x for the first time. First we multiply the current value of changeX by 10. Method setVar stores the updated value into the browser variable x. Finally we use the refresh method to update x. Lastly, on lines 55–59, we update the value of display by concatenating the new value of the browser variable x to its value. Figure 16.9 shows the WML used to call the start function and display the variable display. When display is referenced on line 15, it contains a string made up of the substrings that were concatenated by each function. 1 <?xml version="1.0"?> 2 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD 3 WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd"> 45 <!-- Fig. 16.9: fig16_9.wml --> 6 <!-- A scoping example --> 78 <wml> 9 <card id="index" title="Comparison Program"> 10 <onevent type="onenterforward"> 11 <go href="scoping.wmls#start()"/> 12 </onevent> 13 14 <p> 15 $display 16 </p> 17 </card> 18 </wml> Fig. 16.9 Variable scoping in WML. Chapter 16 WMLScript: Functions 303 © Copyright 2001 by Deitel & Associates, Inc. All Rights Reserved. review Packet 5/7/01. The final output is shown in the screen shots. Notice that the second time functionB is called, the initial value of the browser variable x is 10. Remember, browser variables have a static duration. That is, the value is stored until the document is unloaded from the browser or the browser is exited. On the other hand, the value of x in functionA returns to 25 every time the function is called. This is because local variables have an automatic duration. A local variable’s value is removed from memory as soon as the function that uses it completes its execution.
1089 times read
|