function ClassStudentList() { //[{"Cid":"0d","Students":[{"Sid":"eb7","Sname":"邓小六"},{"Sid":"f5","Sname":"胡小同"}]}] //[{"Cid":"班级Id","Students":[{"Sid":"学生id","Sname":"学生名称"},...]},...] ClassStudentList.CheckedList = []; //静态成员,数组,表示 已经被选中的班级和孩子 var sidList = new Object; sidList.addClassStudent = function(singClassStudents) { ClassStudentList.CheckedList.push(singClassStudents); }; sidList.addStudent = function(cid,sid,sname) { //添加单个学生 //先判断这个班级是否存在,存在的话就直接添加学生,不存在还要先添加班级,再添加学生 var haveClass = false; for (var l = 0; l < ClassStudentList.CheckedList.length; l++) { if (ClassStudentList.CheckedList[l].Cid==cid) { var tempStudentList = ClassStudentList.CheckedList[l].Students; var student = {}; student["Sid"] = sid; student["Sname"] = sname; tempStudentList.push(student); haveClass = true; } } if (!haveClass) { //如果不存在班级,添加班级,添加孩子 var tempStuList = []; var stu = {}; stu["Sid"] = sid; stu["Sname"] = sname; tempStuList.push(stu); var classstudent = {}; classstudent["Cid"] = cid; classstudent["Students"] = tempStuList; this.addClassStudent(classstudent); } }; sidList.deleClass = function(cid) { for (var k = 0; k < ClassStudentList.CheckedList.length; k++) { if (ClassStudentList.CheckedList[k].Cid==cid) { ClassStudentList.CheckedList.baoremove(k); } } }; sidList.deleSingStudent = function(cid,sid) { for (var k = 0; k < ClassStudentList.CheckedList.length; k++) { if (ClassStudentList.CheckedList[k].Cid==cid) { var tempStuList = ClassStudentList.CheckedList[k].Students; for (var kk = 0; kk < tempStuList.length; kk++) { if (tempStuList[kk].Sid==sid) { tempStuList.baoremove(kk); } } if (tempStuList.length==0) { //如果这个班级的学生都删了,就还要删除这个班 this.deleClass(ClassStudentList.CheckedList[k].Cid); } } } }; //显示被选召的孩子 sidList.show = function(tempList) { ShowSingClassStudent(tempList,ClassStudentList.CheckedList); }; return sidList; /*必须*/ }
调用方法:在一个页面的script里面,只new一个对象,这样就能保证这个 类名.属性 就相当于是 c#里面的static静态变量
1:先new一个对象
var car1 = ClassStudentList(); //new 一个列表
2: 调用方法
car1.addClassStudent(classstudent); car1.show(classstudent);
3:通过类名来访问静态属性
ClassStudentList.CheckedList
ClassStudentList.CheckedList.length