home *** CD-ROM | disk | FTP | other *** search
/ Java Internet Programming Reference Guide / Java Internet Programming Reference Guide.iso / autorun / java.dir / 00206_15.txt < prev    next >
Encoding:
Text File  |  1996-01-12  |  9.0 KB  |  544 lines

  1. Statements
  2.  
  3.  
  4.  
  5. JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semi-colon. 
  6.  
  7. Syntax conventions: All keywords in syntax statements are in bold. Words in italics represent user-defined names or statements. Any portions enclosed in square brackets, i.e. [ and ], are optional. {statements} indicates a block of statements, which can consist of a single statement or multiple statements delimited by a curly braces. 
  8.  
  9. The following statements are available in JavaScript: 
  10. break comment continue for for...in function      if...else return var while with 
  11.  
  12.  
  13. ------------------------------------------------------------------------
  14.  
  15.  
  16. break statement
  17.  
  18.  
  19.  
  20. The break statement terminates the current while or for loop and transfers program control to the statement following the terminated loop. 
  21.  
  22. Syntax
  23.  
  24.  
  25.  
  26. break
  27.  
  28.  
  29.  
  30.  
  31. Examples
  32.  
  33.  
  34.  
  35. The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x. 
  36.  
  37. function func(x) { 
  38.     var i = 0; 
  39.     while (i <6) { 
  40.         if (i == 3)  
  41.             break;
  42.         i++;
  43.     }
  44.     return i*x; 
  45.  
  46.  
  47.  
  48.  
  49. ------------------------------------------------------------------------
  50.  
  51.  
  52. comment statement
  53.  
  54.  
  55.  
  56. Comments are notations by the author to explain what the script does, and they are ignored by the interpreter. JavaScript supports Java-style comments: Comments on a single line are preceded by a double-slash (//). Comments that span multiple lines are preceded by a /* and followed by a */. 
  57.  
  58. Syntax
  59.  
  60.  
  61.  
  62. 1. // comment text 
  63. 2. /* multiple line comment text */
  64.  
  65.  
  66.  
  67.  
  68. Examples
  69.  
  70.  
  71.  
  72. // This is a single-line comment.
  73.  
  74.  
  75. /* This is a multiple-line comment. It can be of any length, and 
  76. you can put whatever you want here. */
  77.  
  78.  
  79.  
  80. ------------------------------------------------------------------------
  81.  
  82.  
  83. continue statement
  84.  
  85.  
  86.  
  87. The continue statement terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. In contrast to the break statement, it does not terminate the execution of the loop entirely: instead, 
  88.  
  89. ΓÇóIn a while loop it jumps back to the condition. ΓÇóIn a for loop it jumps to the update expression. 
  90.  
  91.  
  92.  
  93. Syntax
  94.  
  95.  
  96.  
  97. continue
  98.  
  99.  
  100.  
  101.  
  102. Examples
  103.  
  104.  
  105.  
  106. The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12. 
  107.  
  108.  
  109.  
  110. i = 0; 
  111. n = 0; 
  112. while (i <5) { 
  113.     i++; 
  114.     if (i == 3) 
  115.         continue;  
  116.     n += i;
  117.  
  118.  
  119.  
  120.  
  121. ------------------------------------------------------------------------
  122.  
  123.  
  124. for statement
  125.  
  126.  
  127.  
  128. A for loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. The parts of the for statement are: 
  129.  
  130. ΓÇóThe initial expression, generally used to initialize a counter variable. This statement may optionally declare new variables with the var keyword. This expression is optional. ΓÇóThe condition that is is evaluated on each pass through the loop. If this condition is true, the statements in the succeeding block are performed. This conditional test is optional. If omitted, then the condition always evaluates to true. ΓÇóAn update expression generally used to update or increment the counter variable. This expression is optional. ΓÇóA block of statements that are executed as long as the condition is true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements four spaces from the beginning of the for statement. 
  131.  
  132.  
  133.  
  134. Syntax
  135.  
  136.  
  137.  
  138. for ([initial expression]; [condition]; [update expression]) {
  139.     statements
  140. }
  141. initial expression = statement | variable declartion
  142.  
  143.  
  144.  
  145.  
  146.  
  147. Examples
  148.  
  149.  
  150.  
  151. This simple for statement starts by declaring the variable i and initializing it to zero. It checks that i is less than nine, and performs the two succeeding statements, and increments i by one after each pass through the loop. 
  152.  
  153.  
  154. for (var i = 0; i 
  155.  
  156.  
  157.  
  158.  
  159. ------------------------------------------------------------------------
  160.  
  161.  
  162.  
  163.  
  164.  
  165. for...in statement
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. The for statement iterates variable var over all the properties of object obj. For each distinct property, it executes the statements in statements.
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. Syntax
  180.  
  181.  
  182.  
  183.  
  184. for (var in obj) { 
  185.     statements }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. Examples
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.
  201. function dump_props(obj, obj_name) { 
  202.     var result = "", i = "";
  203.     for (i in obj)
  204.         result += obj_name + "." + i + " = " + obj[i] + "\n";
  205.     return result;
  206.  
  207.  
  208.  
  209.  
  210.  
  211. ------------------------------------------------------------------------
  212.  
  213.  
  214.  
  215.  
  216.  
  217. function statement
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225. The function statement declares a JavaScript function name with the specified parameters param. To return a value, the function must have a return statement that specifies the value to return. You cannot nest a function statement in another statement or in itself.
  226.  
  227.  
  228.  
  229. All parameters are passed to functions, by value. In other words, the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. Syntax
  237.  
  238.  
  239.  
  240.  
  241. function name([param] [, param] [..., param]) {
  242.     statements }
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. Examples
  251.  
  252.  
  253.  
  254.  
  255. //This function returns the total dollar amount of sales, when 
  256. //given the number of units sold of products a, b, and c.
  257. function calc_sales(units_a, units_b, units_c) {
  258.     return units_a*79 + units_b*129 + units_c*699
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265. ------------------------------------------------------------------------
  266.  
  267.  
  268.  
  269.  
  270.  
  271. if...else statement
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. The if...else statement is a conditional statement that executes the statements in statements if condition is true. In the optional else clause, it executes the statements in else statements if condition is false. These may be any JavaScript statements, including further nested if statements.
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. Syntax
  287.  
  288.  
  289.  
  290.  
  291. if (condition) {
  292.     statements
  293. } [else {
  294.     else statements
  295. }]
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. Examples
  304.  
  305.  
  306.  
  307.  
  308. if ( cipher_char == from_char ) {
  309.     result = result + to_char; 
  310.     x++
  311. } else  
  312.     result = result + clear_char; 
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. ------------------------------------------------------------------------
  320.  
  321.  
  322.  
  323.  
  324.  
  325. return statement
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332. The return statement specifies the value to be returned by a function. 
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. Syntax
  340.  
  341.  
  342.  
  343.  
  344. return expression;
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352. Examples
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359. The following simple function returns the square of its argument, x, where x is an number.
  360. function square( x ) { 
  361.     return x * x; 
  362.  
  363.  
  364.  
  365.  
  366.  
  367. ------------------------------------------------------------------------
  368.  
  369.  
  370.  
  371.  
  372.  
  373. var statement
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381. The var statement declares a variable varname, optionally initializing it to have value. The variable name varname can be any legal identifier, and value can be any legal expression. The scope of a variable is the current function or, for variables declared outside a function, the current application. 
  382.  
  383.  
  384.  
  385. Using var outside a function is optional; you can declare a variable by simply assigning it a value. However, it is good style to use var, and it is neccessary in functions if there is a global variable of the same name. So, in general, it is a good idea to always use var, but you should definitely use it when declaring a local variable in a function, to ensure that any global variable of the same name does not override it.
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. Syntax
  393.  
  394.  
  395.  
  396.  
  397. var varname [= value] [..., varname [= value] ]
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406. Examples
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413. var num_hits = 0, cust_no = 0 
  414.  
  415.  
  416.  
  417.  
  418. ------------------------------------------------------------------------
  419.  
  420.  
  421.  
  422.  
  423.  
  424. while statement
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. The while statement is a loop that evaluates the expression condition,and if it is true, executes statements. It then repeats this process, as long as condition is true. When condition evaluates to false, execution continues with the next statement following the statements.
  433.  
  434.  
  435.  
  436. Although not required, it is good practice to indent the statements a while loop four spaces from the beginning of the for statement.
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443. Syntax
  444.  
  445.  
  446.  
  447.  
  448. while (condition) {
  449.     statements
  450. }
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459. Examples
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. The following simple while loop iterates as long as n is less than three. Each iteration, it increments n and adds it to x. Therefore, x and n take on the following values 
  467.  
  468.  
  469.  
  470. ΓÇóAfter first pass: x = 1 and n = 1 
  471. ΓÇóAfter second pass: x = 2 and n = 3 
  472. ΓÇóAfter third pass: x = 3 and n = 6 
  473.  
  474.  
  475.  
  476.  
  477.  
  478. After completing the third pass, the condition n <3 is no longer true, so the loop terminates.
  479. n = 0; 
  480. x = 0; 
  481. while( n <3 ) { 
  482.     n ++; x += n; 
  483.  
  484.  
  485.  
  486.  
  487.  
  488. ------------------------------------------------------------------------
  489.  
  490.  
  491.  
  492.  
  493.  
  494. with statement
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502. The with statement establishes object as the default object for the statements. 
  503. Any property references without an object are then assumed to be for object.
  504. Note that the parentheses are required around object.
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511. Syntax
  512.  
  513.  
  514.  
  515.  
  516. with (object){ 
  517.      statements 
  518. }
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526. Examples
  527.  
  528.  
  529.  
  530.  
  531.  
  532. with (Math) { 
  533.     a = PI * r*r
  534.     x = r * cos(theta)
  535.     y = r * sin(theta) 
  536. }
  537.  
  538.  
  539.