0

JavaScript for…in 循环语句

Posted by 撒得一地 on 2016年5月5日 in JavaScript教程

for…in在循环中用来循环遍历对象的属性。因为我们还没有讨论的对象,所以你可能会不理解这种循环。但是一旦你了解对象在 JavaScript 中的行为后,你会发现这种循环非常有用。

语法

for (variablename in object){
   statement or block to execute
}

在每个迭代中,都会将对象的一个属性分配给 variablename 这个变量名,循环一直运行直到该对象的所有属性都被遍历过。

示例

请尝试下面的示例来执行 'for in' 循环。它将打印 web 浏览器的 Navigator 对象。

<html>
  <body>  
    <script type="text/javascript">
      <!--
        var aProperty;
        document.write("Navigator Object Properties<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>

亲自试一试

Output
Navigator Object Properties 
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...

上一篇:

下一篇:

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

4 + 7 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.