我是靠谱客的博主 忧虑茉莉,这篇文章主要介绍js中的for...in语法,现在分享给大家,希望可以做个参考。

for...in语句: 用于遍历对象属性。

 

用for...in遍历"数组": 循环变量x是数组的下标 

复制代码
1
2
3
4
5
6
7
8
///用for...in遍历"数组": 循环变量x是数组的下标 // var arr=[22,33,44,55,66]; for(x in arr){ //Java中的x是数组元素,而js中的x是数组下标 //document.write(x+"&nbsp;"); //下标 : 0 1 2 3 4 document.write(arr[x]+"&nbsp;"); //元素 } document.write("<br/> <hr/>");

用for...in遍历"js对象": 循环变量x是json的成员---属性与方法名

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="text/javascript"> 、 ///用for...in遍历"js对象": 循环变量x是json的成员---属性与方法名 // function Person(name, age){ this.name = name; this.age = age; this.toString = function(){ return this.name + "," + this.age; } } var p = new Person("Jack",22); for(x in p){ //document.write(x+"&nbsp;"); //属性与方法名: name age toString //document.write(p.x+"&nbsp;"); document.write(p[x]+"&nbsp;"); //属性值与方法体 } document.write("<br/>"); </script>

 

最后

以上就是忧虑茉莉最近收集整理的关于js中的for...in语法的全部内容,更多相关js中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部