javascript系列之for in循环

一.欢迎来到我的酒馆

for…in循环通常被用来便利一个对象的属性。当前我们还没有讲到对象,因此你可能会对for…in 循环感到不舒服。但是一旦你理解了对象在javascript中是如何执行的,你会发现for … in 循环非常有用。

目录

    • 一.欢迎来到我的酒馆
    • 二.for...in循环语法

二.for…in循环语法

2.1 for…in循环语法:

for(变量名 in object){
	语句或要执行的代码块
}

在每次迭代中,object的一个属性将会赋值给变量名,这个循环会一直持续下去,直到这个object的所有属性都用尽。

2.2 在代码中使用for…in循环:

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <script language="javascript" type="text/javascript">
        document.write("starting for ... in loop: 
"
); for(let variable in navigator){ document.write("variable = "+variable +"
"
); } document.write("exiting the for ... in loop!");
script> body> html>

输出:

starting for ... in loop:
variable = vendorSub
variable = productSub
variable = vendor
variable = maxTouchPoints
variable = scheduling
variable = userActivation
variable = doNotTrack
variable = geolocation
variable = connection
variable = plugins
variable = mimeTypes
variable = pdfViewerEnabled
variable = webkitTemporaryStorage
variable = webkitPersistentStorage
variable = hardwareConcurrency
variable = cookieEnabled
variable = appCodeName
variable = appName
variable = appVersion
variable = platform
variable = product
variable = userAgent
variable = language
variable = languages
variable = onLine
variable = webdriver
variable = getGamepads
variable = javaEnabled
variable = sendBeacon
variable = vibrate
variable = bluetooth
variable = clipboard
variable = credentials
variable = keyboard
variable = managed
variable = mediaDevices
variable = storage
variable = serviceWorker
variable = virtualKeyboard
variable = wakeLock
variable = deviceMemory
variable = ink
variable = hid
variable = locks
variable = mediaCapabilities
variable = mediaSession
variable = permissions
variable = presentation
variable = serial
variable = gpu
variable = usb
variable = windowControlsOverlay
variable = xr
variable = userAgentData
variable = canShare
variable = share
variable = clearAppBadge
variable = getBattery
variable = getUserMedia
variable = requestMIDIAccess
variable = requestMediaKeySystemAccess
variable = setAppBadge
variable = webkitGetUserMedia
variable = getInstalledRelatedApps
variable = registerProtocolHandler
variable = unregisterProtocolHandler
exiting the for ... in loop!

你可能感兴趣的:(javascript,开发语言,ecmascript)