Nhưng một khi bạn hiểu các đối tượng hoạt động như thế nào trong JavaScript, bạn sẽ thấy vòng lặp này rất hữu ích.
Khóa học java miễn phí |
Cú pháp
==> Khóa học java miễn phífor (variablename in object){ statement or block to execute }Trong mỗi lần lặp, một thuộc tính từ đối tượng được gán cho variablename và vòng lặp này tiếp tục cho đến khi tất cả các thuộc tính của đối tượng bị cạn kiệt.
Thí dụ
Hãy thử ví dụ sau để triển khai vòng lặp 'for-in'. Nó in đối tượng Navigator của trình duyệt web .<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>
Đầu ra
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...
Không có nhận xét nào:
Đăng nhận xét