2012-02-27 98 views
1

关系,我想要实现具有-A在Javascript关系, 在这里有Department对象和部门有多个学生,所以在厅中引用Student对象的数组,实现了,一个在Javascript

这里是代码,

(function(){ 
    alert(" Hi !!!!! "); 

    var engBranch = { 
     compEng : "Bachelor of Engineering in Computer Science", 
     mechEng : "Bachelor of Engineering in Mechanical", 
     cvlEng : "Bachelor of Engineering in Civil", 
     prodEng : "Bachelor of Engineering in Production", 
     elecEng : "Bachelor of Engineering in Electrical", 
     extcEng : "Bachelor of Engineering in Electronics and Telecomminication" 
    }; 



    var StudentIdGenerator = function(studentId,dept){ 
     var _studentId = ""; 

     if(dept == engBranch.compEng){ 
      _studentId = "CS"+studentId; 
     }else if(dept == engBranch.mechEng){ 
      _studentId = "M"+studentId; 
     }else if(dept == engBranch.cvlEng){ 
      _studentId = "CVL"+studentId; 
     }else if(dept == engBranch.prodEng){ 
      _studentId = "P"+studentId; 
     }else if(dept == engBranch.elecEng){ 
      _studentId = "ELC"+studentId; 
     }else if(dept == engBranch.extcEng){ 
      _studentId = "EXTC"+studentId; 
     } 

     return _studentId; 
    } 

    var Student = function(studentName,studentId,department){ 

     var _studentId = StudentIdGenerator(studentId,department); 

     return function(){ 
      this.getStudentName = function(){ 
       return studentName; 
      } 

      this.getStudentId = function(){ 
       return _studentId; 
      } 

      this.display = function(){ 
       return "The Student Name is "+this.getStudentName()+" having ID "+this.getStudentId(); 
      } 
     }; 
    } 

    Student.prototype.display = function(){ 
     return "The Student Name is "+this.getStudentName()+" having ID "+this.getStudentId(); 
    } 

    var Department = function(deptName,deptId){  

     this.getDeptName = function(){ 
      return deptName; 
     } 

     this.getDeptId = function(){ 
      return deptId; 
     } 

     this.setStudents = function(students){ 
      this.students = students; 
     } 

     this.getStudents = function(){ 
      return this.students; 
     } 
    } 

    Department.prototype.display = function(){ 
     var str = "This is department, "+this.getDeptName()+", Students Belonging to this department are \n"; 

     for(var x=0; x < this.getStudents().length;x++){ 
      str += this.getStudents()[x].display()+"\n"; 
     } 

     return str; 
    } 


    var department = new Department(engBranch.cvlEng,"CMP007"); 

    var studentArray = [ 
          new (Student("Jack Nicholson","0021",department.getDeptName()))(), 
          new (Student("Heidi Klum","0043",department.getDeptName()))(), 
          new (Student("Stephen Halkwins","0345",department.getDeptName()))(), 
          new (Student("Jay Leno","1045",department.getDeptName()))(), 
          new (Student("Tomy Lee Johnes","3475",department.getDeptName()))(), 
          new (Student("Oliver Jackson","7345",department.getDeptName()))() 
         ]; 

    department.setStudents(studentArray); 

    alert(department.display()); 

    })(); 

在上面的代码,对象“engBranch”具有所有工科中可用的流。

有一个独立的函数StudentIdGenerator(),它的职责是创建一个StudentId,具体取决于学生所属的部门。

本身返回一个新函数的Student Constuctor函数,请参阅Student函数返回的内容。

这是根本不需要对学生的原型显示功能,请忽略它,

部门构造器功能,

而且在系的原型链中的显示功能,

所以,我的问题是,我的例子是否是一个适当的拥有一对多关系的实现。

我可以做一些新的或额外的下面的代码。

等待你的答复

+0

你试图实现哪种特定的关系? – Deestan 2012-02-27 14:17:31

+0

部门与学生有关,部门与很多学生有关,即一个部门可以有很多学生 – 2012-02-27 14:22:56

+1

(我通过upvoting将问题逆转-1,我也看到问题的问题,因为他似乎已经开始远离任何一条好路径 - 对于一个简单问题来说非常复杂的方法,这似乎乍一看,然而,如何通过良好的建议而不是惩罚来帮助教育,并用(downvote)棍子殴打他?在低(呃)水平。) – 2012-02-27 15:18:52

回答