JS forEach()第二個參數

定義和用法

forEach() 方法用於調用數組的每一個元素,並將元素傳遞給回調函數。javascript

注意: forEach() 對於空數組是不會執行回調函數的。html

語法

array.forEach(function(currentValue, index, arr), thisValue)
複製代碼

實例

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			[1, 2, 3, 4, 5].forEach(function(a) {
				console.log(a, this)
			})
		</script>
		<script type="text/javascript">
			[1, 2, 3, 4, 5].forEach(function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.call([1, 2, 3, 4, 5], function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.apply([1, 2, 3, 4, 5], [function(a) {
					console.log(a, this)
				},
				[6, 7, 8, 9]
			])
		</script>
		<script type="text/javascript">
			Array.prototype.forEach.bind([1, 2, 3, 4, 5])(function(a) {
				console.log(a, this)
			}, [6, 7, 8, 9])
		</script>
	</head>

	<body>
	</body>

</html>
複製代碼

相關文章
相關標籤/搜索