–Duration (orlifetime) is the period during which identifierexists in memory.

•Some identifiers exist briefly

•Some identifiers are repeatedly created and destroyed

•Some identifiers exist for entire execution of the program

•Identifiers which represent local variables in a functionhaveautomatic duration

–Automatically created when program control enters function

–Exist while function is active

–Automatically destroyed when function is exited

–Referred to aslocal variables



2001 Deitel & Associates, Inc. All rights reserved.

36

16.7 Duration of Identifiers

•JavaScript also has identifiers ofstatic duration

–Typically defined in <HEAD> section of HTML document

–Exist from point at which declared until browsing sessionover

–Even though they exist after <HEAD> section terminates,cannot necessarily be used throughout the script

–Referred to asglobal variables

orscript-level variables



2001 Deitel & Associates, Inc. All rights reserved.

37

16.8 Scope Rules

•Scope

of identifier is portion of program in whichidentifier can be referenced

–Local variable declared in a function can be used only in thatfunction

•Identifiers declared inside a function havefunction

(orlocal)scope

–Begins with opening brace ({) of function

–Ends with closing brace(}) of function

–Function parameters also have local scope

–If local variable has same name as global variable, globalvariable “hidden” from body of function



2001 Deitel & Associates, Inc. All rights reserved.

Outline

1.1 Initialize variable

2.1 Definestart()

function

2.2 Statestart

function actions

2.3 Call user definedfunctions

2.4 Print results

3.1 Define functionfunctionA()

1

<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">

2

<HTML>

3

<!--

Fig. 16.7: scoping.html-->

4

5

<HEAD>

6

<TITLE>A Scoping Example</TITLE>

7

8

<SCRIPT LANGUAGE =

"JavaScript">

9

var

x = 1;// global variable

10

11

function

start()

12

{

13

var

x = 5;// variable local to function start

14

15

document.writeln( "local x in start is " + x );

16

17

functionA();// functionA has local x

18

functionB();// functionB uses global variable x

19

functionA();// functionA reinitializes local x

20

functionB();// global variable x retains its value

21

22

document.writeln(

23

"<P>local x in start is " + x + "</P>" );

24

}

25

26

function

functionA()

27

{

28

var

x = 25;// initialized each time functionA is called

29



2001 Deitel & Associates, Inc. All rights reserved.

Outline

3.2 Demonstratefunction scope

4.1 DefinefunctionB()

4.2 Demonstrate globalscope

34

" before exiting functionA</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 " + x +

43

" on exiting functionB</P>" );

44

}

45

</SCRIPT>

46

47

</HEAD>

48

<BODY ONLOAD ="start()"></BODY>

49

</HTML>

30

document.writeln( "<P>local x in functionA is " + x +

31

" after entering functionA" );

32

++x;

33

document.writeln( "<BR>local x in functionA is " + x +



2001 Deitel & Associates, Inc. All rights reserved.

40

Script Output:



2001 Deitel & Associates, Inc. All rights reserved.

41

16.9 JavaScript Global Functions

•Global functions are part

of

JavaScript’s

Global

object

–Contains all global variables in the script

–Some programmers refer to these functions as methods

•Global functions and user-defined functions part ofGlobal

object

•You do not need to use theGlobal

object directly

–JavaScript does this for you



2001 Deitel & Associates, Inc. All rights reserved.

42

16.9 JavaScript Global Functions

Global function

Description

escape

This function takes a string argument and returns a string in which allspaces, punctuation, accent characters and any other character that isnot in the ASCII character set (see Appendix C, “ASCII CharacterSet”) are encoded in a hexadecimal format (see the “Number Systems”document on the CD that accompanies this book) that can berepresented on all platforms.

eval

This function takes a string argument representing JavaScript code toexecute. The JavaScript interpreterevaluates the code and executes itwhen theevalfunction is called. This function allows JavaScriptcode to be stored as strings and executed dynamically.

isFinite

This function takes a numeric argument and returnstrueif the valueof the argument is notNaN,Number.POSITIVE_INFINITYorNumber.NEGATIVE_INFINITY; otherwise the function returnsfalse.

isNaN

This function takes a numeric argument and returnstrueif the valueof the argument is not a number; otherwise the function returnsfalse. The function is commonly used with the return value ofparseIntorparseFloatto determine whether the result is aproper numeric value.



2001 Deitel & Associates, Inc. All rights reserved.

43

16.9 JavaScript Global Functions

Global function

Description

parseFloat

This function takes a string argument and attempts to convert thebeginning of the string into a floating-point value. If the conversion isnot successful, the function returnsNaN; otherwise, it returns theconverted value (e.g.,parseFloat(

"abc123.45"

)returnsNaN

andparseFloat(

"123.45abc"

)returns the value123.45.

parseInt

This function takes a string argument and attempts to convert thebeginning of the string into an integer value. If the conversion is not

successful, the function returnsNaN; otherwise, it returns the convertedvalue (e.g.,parseInt(

"abc123"

)returnsNaNandparseInt(

"123abc"

)returns the integer value123. This function takes anoptional second argument from 2 to 36 specifying theradix(orbase) ofthe number. Base 2 indicates that the first argument string is inbinary

format, 8 indicates that the first argument string is inoctalformat and16 indicates that the first argument string is inhexadecimalformat. Seesee the “Number Systems” document on the CD that accompanies thisbook for more information on binary, octal and hexadecimal numbers.

unescape

This function takes a string as its argument and returns a string inwhich all characters that we previously encoded withescapearedecoded.