•For…in声明用来遍历数组元素或对象属性
•For...In声明中的代码每执行一次,就会对数组元素或对象属性进行一次操作
•数组有几个元素或者对象有几个属性,循环执行几次
例子:
var x;
var cars=new Array();
cars[0]='夏利';
cars[1]='奥拓';
cars[2]='时风';
//如果是数组的话,X变量就是数组的下标,数组有几个元素,就循环几次,每循环一次就会对数组的元素进行操作
for(x in cars)
{
document.write(cars[x]+'<br />');
}
var cars=new Array();
cars[0]='夏利';
cars[1]='奥拓';
cars[2]='时风';
//如果是数组的话,X变量就是数组的下标,数组有几个元素,就循环几次,每循环一次就会对数组的元素进行操作
for(x in cars)
{
document.write(cars[x]+'<br />');
}
Try…catch
作用是测试代码中的错误
在网页中捕获错误的方法:
1,try…catch…语句
2,throw()抛出异常
例子:
var x=prompt("enter number between 0 and 10");
try{
if(x<0)
{//throw把这个错误抛出去
throw 'err1';}
else if(x>10)
{throw 'err2';}}
catch(err)
{
if(err=='err1')
{
document.write('the value is too low');
}else if(err=='err2'){
document.write('the value is too high');
}
}
try{
if(x<0)
{//throw把这个错误抛出去
throw 'err1';}
else if(x>10)
{throw 'err2';}}
catch(err)
{
if(err=='err1')
{
document.write('the value is too low');
}else if(err=='err2'){
document.write('the value is too high');
}
}