JavaScript 中的forEach函数

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script>
    function Person(name,age){
      this.name =name
      this.age = age
    }
    const per = new Person('孙悟空',25)
    const per1 = new Person('蜘蛛金',15)
    const per2 = new Person('猪八戒',24)
    const per3 = new Person('二郎神',8)
    const per4 = new Person('红孩儿',10)
    const per5 = new Person('杨戬',38)
    const arry = [per,per1,per2,per3,per4,per5]
    const newArry = []
    for(let i=0;i<arry.length;i++){
      if (arry[i].age>20){
        newArry.push(arry[i])
      }
    }
    // console.log(newArry);
    // forEach函数
    // 这种由我们创建的函数,但是不是由我们调用的函数叫做回调函数。
    arry.forEach(function(item){

      console.log('hello',item);
    })
  </script>
</head>
<body>
  
</body>
</html>
原文地址:https://www.cnblogs.com/ch2020/p/15711573.html