Section 5.8. Miscellaneous Statements


5.8. Miscellaneous Statements


These are comment statements that let you enter nonexecuting text in a script. Any text following the // symbol anywhere in a statement line is ignored by the language interpreter. The next line of script, unless it begins with another // symbol, is interpreted by the browser.

For multiline comment blocks, you can begin a block with the /* symbol. A comment block may encompass multiple source code lines. The block is closed with the */ symbol, after which the interpreter engages subsequent statements.


Example

 // convert temp from C to F /* many lines of comments */ 


IE for Windows includes a scripting feature called conditional compilation. It is a mode that, once turned on via the @cc_on statement, allows JScript statements to run under conditions that are testable within this conditional environment. If you surround conditional compilation statements by JavaScript comments, the conditional statements run only in IE 4 or later for Windows, while not conflicting with other browsers.

The "conditional" part comes from numerous global properties (all preceded with the @ symbol) that reveal environmental properties, such as script engine version, operating system, and CPU type. All of this information is available from the navigator object's properties on a wide range of browsers, so this is not unique information available only to this conditional environment.

To engage conditional compilation, include the following statement in your script:

 /*@cc_on @*/ 

This is a one-way toggle: once the mode is turned on, it can't be turned off in the current page.

The following fragment shows how the @if and related statements display some environmental information in the window's status bar if the browser is running JScript Version 5.6 or later (IE 6 or later):

 /*@cc_on @*/ /*@if (@_jscript_version >= 5.6 && @_x86)     status = "Now running JScript version " + @_jscript_version +     " with Intel inside.";    @else @*/     status = "Have a nice day."; /*@end @*/ 

The @set statement lets you assign a numeric or Boolean value (no strings) to a variable (a variable with an @ prefix) within a conditional compilation section:

 @set @isOK = @_win32 

Once initialized, that variable (including its otherwise unacceptable identifier) can be used in script statements throughout the page. Note that the Visual Basic-inspired syntax of @ statements in conditional compilation statements does not permit semicolons at the end of statements.

On the one hand, conditional compilation could be useful for IE-only deployment to screening older IE versions from new language features that would generate compilation errors (such as try-catch constructions) because such statements compile only under very controllable version situations. In a multibrand browser development shop, however, at most you might find application for IE-only debugging purposes, but probably not for actual application deployment.


Example

See the discussion above.


The function keyword begins a named function definition. For anonymous functions, see the Function object.


Example

 function myFunc(arg1, arg2) {     // function statements here } 


The let keyword can be used in a few ways to define variables with a scope separate distinct from other global or local scope within the script. For example, you can limit variable scope to a single expression by first defining the variables inside parentheses, and then using those variables in a single statement, as in the following statements residing inside a function:

 // local variables var x = 1; var y = 10; alert(let(x = 10, y = y * 5) x*y); alert(x*y); 

In the first alert, the computed value is 500, while the second alert displays 10.

If you use a let statement inside a block (e.g., inside braces), the scope of the variable, constant, or function defined via let is within that block, allowing those defined values to be used locally within the block. You can also use the let statement to define for loop counter variables that won't collide with other loops in the same function.


Example

 for (let i = 0; i < myArray.length; i++) {     // statements here using i as index variable } 


A keyword that defines the creation of a new variable. Although the keyword is optional for global variables (those not declared or initialized inside a function), it is good form to use this keyword for each new variable. Using the var keyword inside a function makes the variable local to statements inside the function.

You may simply declare one or more variable names, in which case their initial values are null. Or you can also initialize a new variable with a value.


Example

 var a, b, c; var myName = "Susan"; 




Dynamic HTML. The Definitive Reference
Dynamic HTML: The Definitive Reference
ISBN: 0596527403
EAN: 2147483647
Year: 2004
Pages: 120
Authors: Danny Goodman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net