Implementing has-a relationship in Javascript
<p>I want to implement has-a relationship in Javascript, 
Here there is Department object, and Department has multiple students, so there is array of Student objects referenced in Department,</p>

<p>Here is the code,</p>

<pre><code>(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 &lt; 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());

   })();
</code></pre>

<p>In the above code, object "engBranch" has all the streams available in Engineering Course.</p>

<p>There is a seperate function StudentIdGenerator() whose responsibility is to create a StudentId depending on to which department, student belongs to.</p>

<p>Student Constuctor function which itself returns a new function, please see what Student function returns.</p>

<p>A prototype display function on Student which is not at all required, please ignore it,</p>

<p>A Department contructor function,</p>

<p>And a display function in the prototype chain of Department,</p>

<p>So, my question is, did my example is a proper has-a implementation of one to many relation.</p>

<p>Can i do something new or extra to the below code.</p>

<p>waiting for your reply </p>
