for ... in 循環(huán)用于循環(huán)訪問對象的屬性,由于無涯教程尚未討論Objects對象,您就會發(fā)現(xiàn)此循環(huán)非常有用。
“ for...in”循環(huán)的語法為:for (variablename in object) { statement or block to execute }
在每次迭代中,將 object對象中的一個屬性分配給 variablename 變量,此循環(huán)一直進(jìn)行到對象的所有屬性。
請嘗試以下示例來實(shí)現(xiàn)" for-in"循環(huán),它將打印網(wǎng)絡(luò)瀏覽器的 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>
運(yùn)行上面代碼輸出文章來源:http://www.zghlxwxcb.cn/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文章來源地址http://www.zghlxwxcb.cn/news/detail-595755.html
到了這里,關(guān)于無涯教程-Javascript - For...in循環(huán)語句的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!