js 关键字 in 的使用方法

1.for in 循环 数组或对象属性迭代

[pcsh lang="js" tab_size="4" message="" hl_lines="" provider="manual"]

var x  
var mycars = new Array()  
mycars[0] = "Saab"  
mycars[1] = "Volvo"  
mycars[2] = "BMW"  
  
for (x in mycars)  
{  
    document.write(mycars[x] + "<br />")  
}  

[/pcsh]

2.判断对象是否为数组/对象的元素/属性:

[pcsh lang="js" tab_size="4" message="" hl_lines="" provider="manual"]

var arr = ["a","b","2","3","str"];  
var result = ("b" in arr);  
var result1 = (4 in arr);  
document.write(result+"<br>");  
document.write(result1+"<br>");  

[/pcsh]

输出结果:

false
true

 

 

此处评论已关闭