自己编写each函数

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style>
		div{padding:20px;}
	</style>
</head>
<body style="overflow:hidden;">
	<script>
		var each = function(arr, fn){
			for(var index in arr){
				fn.apply(null,[index,arr[index]]);
				// fn.call(null,index,arr[index]);
			}
		}
		var myArr = [1, 5, 6, 8];
		each(myArr, function(index, item){
			item++;
			console.log(item)
		})
	</script>
</body>
</html>

  

posted @ 2018-03-27 18:26  幽竹小妖  阅读(103)  评论(0编辑  收藏  举报