-
Notifications
You must be signed in to change notification settings - Fork 0
/
42.forEach.html
45 lines (42 loc) · 1.81 KB
/
42.forEach.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
一般我们都是使用for循环去遍历数组
JS中还为我们提供了一个方法,用来遍历数组
forEach()
- 这个方法只支持IE8以上的浏览器
*/
//创建一个数组
var arr=["孙悟空","猪八戒","白骨精","沙和尚","唐僧"];
/*
forEach()方法需要一个函数作为参数
- 像这种函数,由我们创建但是不由我们调用的,我们称为回调函数
- 数组中有几个元素函数就会执行几次,每次执行时,浏览器会将遍历到的元素
以实参的形式传递进来,我们可以来定义形参来读取这些内容
- 浏览器会在回调函数中传递三个参数:
第一个参数,就是当前正在遍历的元素
第二个参数,就是当前正在遍历的元素的索引
第三个参数,就是当前正在遍历的数组
*/
/*
一般我们都是使用for循环去遍历数组
JS中还为我们提供了一个方法,用来遍历数组
foreach()
- 这个方法只支持IE8以上的浏览器
IE8及以下的浏览器均不支持该方法,所以如果需要兼容IE8,则不要使用forEach()
还是使用for循环来遍历
*/
arr.forEach(function (value,index,obj) {
console.log("a="+ value,"b="+ index,"c="+ obj);
// console.log(c==arr);
});
</script>
</head>
<body>
</body>
</html>