home *** CD-ROM | disk | FTP | other *** search
/ Internet Toolkit for Windows 95 / Inter95.iso / progs / tcd32205 / JAVSCRIP.HT_ / JAVSCRIP.HT
Encoding:
Text File  |  1996-09-28  |  12.2 KB  |  607 lines

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