forin forof forEach myForEach

2020-10-28 10:19:34 浏览数 (1)

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		let arr=[1,3,5,7,9];
		for(let i=0;i<arr.length;i  )
		{
             //console.log(arr[i]);
		}
		 // 2.利用forin循环来遍历数组
		 for(let key in  arr)
		 {
            // console.log(arr[key]);
		 }
		 function Person()
		 {
		 	this.name = "lnj";
            this.age = 34;
            this.score = 99;
		 }
		  let p = new Person();
        //console.log(p);
        // 3.利用ES6中推出的for of循环来遍历数组
        for(let value of arr)
        {
            console.log(value);
        }
       	// 4.还可以利用Array对象的forEach方法来遍历数组
       	/*arr.forEach(function(a,b,c)
       	{
       		console.log(a,b,c);
       	});*/
       	Array.prototype.myForEach=function(fn)
       	{
       		for(let i=0;i<this.length;i  )
       		{
       			fn(this[i],i,this);
       		}
       	}
       	arr.myForEach(function(a,b,c)
       	{
       		console.log(a,b,c);
       	});
		
		
	</script>
</body>
</html>

0 人点赞