现在的位置: 首页 > 综合 > 正文

JavaScript Statements (语句)

2013年10月04日 ⁄ 综合 ⁄ 共 1533字 ⁄ 字号 评论关闭

Statement

Syntax

Purpose

break
break;

break labelname;

Exit from the innermost loop or switch statement or from the statement named by label.

case
case expression:

Label a statement within a switch statement.

continue
continue; 

continue labelname;

Restart the innermost loop or the loop named by label.

default
default: 

Label the default statement within a switch statement.

do/while
do

    statement

while (expression);

An alternative to the while loop.

empty
;

Do nothing.

for
for (initialize ; test ; increment)

    statement

An easy-to-use loop.

for/in
for (variable in object)

    statement

Loop through the properties of an object.
遍历对象的所有属性:
<script>
var o = {a:'aa', b:'bb', c:'cc'};
for(var p in o)
{
  alert(eval("o." + p));
}
</script>

遍历数组的所有元素:
<script>
var arr = ['11', '22', '33'];
for(var i in arr)
{
  alert(arr[i]);
}
</script>

function
function funcname([arg1[..., argn]]) {

    statements

}

Declare a function.

if/else
if (expression)

    statement1

[else statement2]

Conditionally execute code.

label
identifier: statement

Give statement the name identifier.

return
return [expression];

Return from a function or return the value of expression from a function.

switch
switch(n)
{
case 1:
  execute code block 1
  break;
case 2:
  execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}

Multiway branch to statements labeled with case or default: .

throw
throw exception

Throw an exception.
Sample:
throw new Error("error occurs.");

try...catch
try
 {
  //Run some code here
 }
catch(err)
 {
  //Handle errors here
 }
finally
{ statements }

Catch an exception.

var
var name_1 [ = value_1]

[ ..., name_n [ = value_n]];

Declare and initialize variables.

while
while (expression)

    statement

A basic loop construct.

with
with (object)

    statement

Extend the scope chain. (Deprecated.)

抱歉!评论已关闭.