Skip to content

Instantly share code, notes, and snippets.

@M3kH
Last active Aug 10, 2017
Embed
What would you like to do?
OOP - HYF Class
function Person( lastName, name ){
this.lastName = lastName;
this.name = name;
this.getLastName = function(){
return this.lastName || 'Not defined';
};
this.getName = function(){
return this.name || 'Not defined';
}
}
function Teacher( lastName, name ){
Person.call(this, lastName, name);
return this;
}
function Student(lastName, name){
Person.call(this, lastName, name);
this.homework = [];
return this;
}
function HYFClass(){
this.members = [];
this.addMember = function( instanceOfPerson ){
this.members.push( instanceOfPerson );
}
this.getAllMemberName = function(){
// TODO
}
this.getAllMemberLastName = function(){
for(var k=0; k<this.members.length; k++){
// FIX to return values
console.log(this.members[k].getLastName());
}
}
this.getStudents = function(){
for(var k=0; k< this.members.length; k++){
if(this.members[k] instanceof Student)
// TODO
}
}
this.getTeachers = function(){
// TODO
}
}
var members = [
{name: 'Mauro', lastName: 'Mandracchia', type: 'teacher'},
{name: 'Ali', lastName: 'Barakat', type: 'student'}
];
var JS3 = new HYFClass();
for(var k=0; k<members.length; k++){
var member = members[k];
if(member.type === 'teacher'){
JS3.addMember( new Teacher(member.lastName, member.name) );
}else{
JS3.addMember( new Student( member.lastName, member.name) )
}
}
JS3.getAllMemberLastName();

Assignment in the class

  • HYFClass should have a method that returns all the name of the members.
  • HYFClass should have a method that returns only Students and one that returns only Teachers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment