混合模式造对象实例一

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

	
		//构造函数
		//注册
		function Register(){
			this.arr=[];//属性
		}
		Register.prototype.loadInfo=function(){
			while(confirm('是否继续录入学生信息?')){										
					var name=prompt('请输入姓名:');
					var age=prompt('请输入年龄:');
					var no=prompt('请输入学号:');
					var stu=new Student(no,name,age);//创建学生对象				
					//保存对象
					this.arr.push(stu);
				}
		}
		Register.prototype.showInfo=function(){
				this.arr.forEach(function(item){
					console.log(item.name,item.age,item.no)
				});
			};
		var reg=new Register();//创建对象实例
	    reg.loadInfo();//录入数据
		reg.showInfo();


		//创建学生
		function Student(no,name,age){
			this.no=no;
			this.name=name;
			this.age=age;
		}
	</script>	
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44606660/article/details/87862395
今日推荐