Цикл for ... in используется для прокрутки свойств объекта. Поскольку мы еще не обсуждали объекты, вам может не понравиться этот цикл. Но как только вы поймете, как объекты ведут себя в JavaScript, вы найдете этот цикл очень полезным.

Синтаксис
for (variablename in object){
   statement or block to execute
}

На каждой итерации одно свойство объекта присваивается переменной variablameame, и этот цикл продолжается до тех пор, пока все свойства объекта не будут исчерпаны.

пример

Попробуйте следующий пример для реализации цикла for-in. Он печатает объекты Navigator веб-браузера.

<html>
   <body>
     
      <script type="text/javascript">
         <!--
            var aProperty;
            document.write("Navigator Object Properties
 ");
        
            for (aProperty in navigator) {
               document.write(aProperty);
               document.write("
");
            }
            document.write ("Exiting from the loop!");
         //-->
      </script>
     
   </body>
</html>
Результат
Navigator Object Properties
vendorSub
productSub
vendor
maxTouchPoints
hardwareConcurrency
cookieEnabled
appCodeName
appName
appVersion
platform
product
userAgent
language
languages
onLine
doNotTrack
geolocation
mediaDevices
connection
plugins
mimeTypes
webkitTemporaryStorage
webkitPersistentStorage
serviceWorker
getBattery
sendBeacon
getGamepads
getUserMedia
webkitGetUserMedia
javaEnabled
vibrate
requestMIDIAccess
budget
permissions
presentation
registerProtocolHandler
unregisterProtocolHandler
deviceMemory
clipboard
storage
credentials
usb
requestMediaKeySystemAccess
mediaCapabilities
Exiting from the loop!



Понравилась статья? Поделитесь ею с друзьями и напишите отзыв в комментариях!

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.

Ok