JavascriptFor...in循环语句

JavascriptFor...in循环语句 首页 / JavaScript入门教程 / JavascriptFor...in循环语句

for ... in 循环用于循环访问对象的属性,由于无涯教程尚未讨论Objects对象,您就会发现此循环非常有用。

“ for...in”循环的语法为:
for (variablename in object) {
   statement or block to execute
}

在每次迭代中,将 object对象中的一个属性分配给 variablename 变量,此循环一直进行到对象的所有属性。

请尝试以下示例来实现" for-in"循环,它将打印网络浏览器的 Navigator 对象。

<html>
   <body>      
      <script type = "text/javascript">
         <!--
            var aProperty;
            document.write("NavigatorObjectProperties<br/> ");        
            for (aProperty in navigator) {
               document.write(aProperty);
               document.write("<br/>");
            }
            document.write ("Exiting from the loop!");
         //-->
      </script>      
      <p>Set the variable to different object and then try...</p>
   </body>
</html>

运行上面代码输出

NavigatorObjectProperties 
serviceWorker 
webkitPersistentStorage 
webkitTemporaryStorage 
geolocation 
doNotTrack 
onLine 
languages 
language 
userAgent 
product 
platform 
appVersion 
appName 
appCodeName 
hardwareConcurrency 
maxTouchPoints 
vendorSub 
vendor 
productSub 
cookieEnabled 
mimeTypes 
plugins 
javaEnabled 
getStorageUpdates 
getGamepads 
webkitGetUserMedia 
vibrate 
getBattery 
sendBeacon 
registerProtocolHandler 
unregisterProtocolHandler 
Exiting from the loop!
Set the variable to different object and then try...

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

说透芯片 -〔邵巍〕

Python自动化办公实战课 -〔尹会生〕

OAuth 2.0实战课 -〔王新栋〕

微信小程序全栈开发实战 -〔李艺〕

Kafka核心源码解读 -〔胡夕〕

说透中台 -〔王健〕

即时消息技术剖析与实战 -〔袁武林〕

深入浅出计算机组成原理 -〔徐文浩〕

左耳听风 -〔陈皓〕

好记忆不如烂笔头。留下您的足迹吧 :)