
常用软件类: |
|杀毒安全 | |联络聊天 | |网络软件 | |多媒体类 | |系统工具 | |图形图像 | |系统工具 | |应用软件 | |行业软件 |
开发设计类: |
|动画制作 | |图像处理 | |3D设计 | |操作系统 | |站长学院 | |网络相关 | |WEB设计 | |数据库类 | |程序开发 |
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.
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.
The following statements are available in JavaScript:
|
|
|
The break statement terminates the current while or for loop and transfers program control to the statement following the terminated loop.
break
The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x.
function func(x) {
var i = 0;
while (i < 6) {
if (i == 3)
break;
i++;
}
return i*x;
}
Comments are notations by the author to explain what the script does, and they are ignored by the interpreter. JavaScript supports Java-style comments:
1. // comment text 2. /* multiple line comment text */
// This is a single-line comment.
/* This is a multiple-line comment. It can be of any length, and you can put whatever you want here. */
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,
continue
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.
i = 0;
n = 0;
while (i < 5) {
i++;
if (i == 3)
continue;
n += i;
}
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:
for ([initial expression]; [condition]; [update expression]) {
statements
}
initial expression = statement | variable declartion
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.
for (var i = 0; i < 9; i++) {
n += i;
myfunc(n);
}
The for statement iterates variable var over all the properties of object obj. For each distinct property, it executes the statements in statements.
for (var in obj) {
statements }
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.
function dump_props(obj, obj_name) {
var result = "", i = "";
for (i in obj)
result += obj_name + "." + i + " = " + obj[i] + "\n";
return result;
}
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.
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.
function name([param] [, param] [..., param]) {
statements }
//This function returns the total dollar amount of sales, when
//given the number of units sold of products a, b, and c.
function calc_sales(units_a, units_b, units_c) {
return units_a*79 + units_b*129 + units_c*699
}