Javascript pocket reference 3rd edition v413hav

Page 82

try { // Ask the user to enter a number var n = Number(prompt("Enter an number", "")); // Compute the factorial of the number, // assuming the input is valid. var f = factorial(n); // Display the result alert(n + "! = " + f); } catch (ex) { // We end up here on invalid input. alert(ex); // Tell the user what the error is. }

Miscellaneous Statements This section describes the remaining three JavaScript statements—with, debugger, and use strict.

with When JavaScript looks up the value of a variable, it first looks at the variables defined within the current function, then (if the function is nested) at variables defined in enclosing functions and finally at global variables. The with statement temporarily alters the way variables are looked up by specifying an object whose properties should be treated as if they were variables. It has the following syntax: with (object) statement

This statement executes statement somewhat as if it was the body of a nested function and the properties of object were parameters passed to that function. The with statement is forbidden in strict mode (see “use strict”) and should be considered deprecated in nonstrict mode: avoid using it whenever possible. JavaScript code that uses with is difficult to optimize and is likely to run much more slowly than the equivalent code written without the with statement.

70 | Chapter 4: Statements


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.