javaScript 中的for...in循环
2012-09-02 09:46
645 查看
JavaScript For...In Statement
The for...in statement loops throughthe properties of an object.
打印该对象的所有属性
Syntax
for (variable in object)
{
code to be executed
}
Note: The block ofcode inside of the for...in loop will be executed once for each property.
Example
Looping through the properties of anobject:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the properties of an object named "person".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>
结果:
JohnDoe25
The for...in statement loops throughthe properties of an object.
打印该对象的所有属性
Syntax
for (variable in object)
{
code to be executed
}
Note: The block ofcode inside of the for...in loop will be executed once for each property.
Example
Looping through the properties of anobject:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the properties of an object named "person".</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>
结果:
JohnDoe25
相关文章推荐
- javascript中for/in循环介绍及使用技巧
- javascript 循环语句 while、do-while、for-in、for用法区别
- 【JavaScript】学习笔记(对象的访问,创建;for-in循环)
- JavaScript中for..in循环陷阱介绍
- JavaScript里的循环方法:forEach,for-in,for-of
- javascript 循环语句 while、do-while、for-in、for用法区别
- javascript中for/in循环及使用技巧
- JavaScript中for..in循环陷阱介绍
- JavaScript 利用for-in循环数组或对象
- JavaScript中for..in循环陷阱介绍
- [Effective JavaScript 笔记]第49条:数组迭代要优先使用for循环而不是for...in循环
- javascript中for/in循环及使用技巧
- JavaScript for...in 循环的陷阱
- java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)
- JavaScript for…in 循环的陷阱
- JavaScript里的循环方法:forEach,for-in,for-of
- JavaScript for...in循环
- Javascript 中的for…in…循环
- JavaScript学习--Item16 for 循环和for...in 循环的那点事儿
- JavaScript中for..in循环的陷阱