javascript基础(伪数组)

函数sum没有显式的形参,而我们又可以动态的传递给其任意多的参数,那么,如何在sum函数中如何引用这

些参数呢?这里就需要用到arguments这个伪数组了

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>js-arguments</title>
	<script type="text/javascript">
	   function sum(){
	   var result=0;
	   for(var i=0,len=arguments.length;i<len;i++){
		  var current=arguments[i];
	      if(isNaN(current)){
			   throw new Error('not a number exception!');
	       }
		  else{
		    result+=current;
		  }
	   }
	  console.log(result);
      }
	 /////test
	   sum(1,323,34,343,43,4);
	   sum('dfdfdf',34,343,343);
	   sum('dfd','fdfd');
	</script>
</head>
<body>
</body>
</html>

你可能感兴趣的:(javascript基础(伪数组))