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>
运行上面代码输出文章来源:https://www.toymoban.com/news/detail-595755.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...
参考链接
https://www.learnfk.com/javascript/javascript-forin-loop.html文章来源地址https://www.toymoban.com/news/detail-595755.html
到了这里,关于无涯教程-Javascript - For...in循环语句的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!