构造函数造对象实例二

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		/*
			录入班级学生(姓名、年龄、学号),输出所有学生的学号、姓名和年龄
		*/

		var a=new regData();
		a.writeInfo();
		a.showData();

		//录入数据
		function regData() {
			this.arr=[];
			this.writeInfo=function(){
				while(confirm("是否继续录入?")){
				var name=prompt('请输入姓名:');
				var age=prompt('请输入年龄:');
				var no=prompt('请输入学号:');
				var stu=new Student(no,name,age);//创建对象				
				//保存对象
				this.arr.push(stu);
			    }   
			};
			this.showData=function(){
				this.arr.forEach(function(item){
				console.log(item.no,item.name,item.age);
			});
			};
		}
		//创建学生
		function Student(no,name,age){
			this.no=no;
			this.name=name;
			this.age=age;
		}
	
	</script>	
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44606660/article/details/87856490