Javascript中使用For In遍历对象
程序员文章站
2022-05-23 13:04:25
...
Javascript中的For in类似于Java中增强的For循环,非常好用。。
格式:
for(var variable in obj) { statement }
例子:
<script language="javascript" type="text/javascript"> function Apple() { this.color = 'Red'; this.weight = '10kg'; this.name = 'Red Apple'; } var apple = new Apple(); //definition prop as temp var for (var prop in apple) { // get property name by prop // get property value by obj[prop] document.write( prop + " : "+ apple[prop] + "<br>") } </script>