New guy at Javascript here trying to get better at coding with this language. So im trying to create an object that is supposed to be a Dental Clinic. Inside that clinic object i want an array of all the dentists that works there. The dentists are also object(so objects inside an object). When i try to add/acess the objects they are undefined and it tells me that the methods they have do not exist. Im not sure what the error could be, and im used to coding it this way from my time with Java. Can anyone check out the code and point me in the right direction? Appriciate any help.
function createDentistObject(firstName, lastName, age)
{
let dentistObject =
{
firstName: firstName, lastName: lastName, age:age,
getFullName: function()
{
return this.firstName + " " + this.lastName;
}
};
}
function createDentistList()
{
let dentistList = [(createDentistObject("Test","Test",0))];
return dentistList;
}
let myDentalClinic =
{
dentists:createDentistList(),
addDentist:function(dentistObject)
{
console.log("Dentistfullname: " + this.dentistObject.getFullName());
if(this.dentists.length == 0)
{
this.dentists.push(this.dentistObject);
}
else
{
/*if(this.dentistExist(dentistObject))
{
console.log("Dentist exists in the list!");
}*/
this.dentists.push(dentistObject);
}
},
dentistExist:function(dentistObject)
{
for(let i = 0; i < this.dentists.length; i++)
{
if(this.dentists[i].firstName === dentistObject.firstName)
{
return true;
}
}
return false;
},
printAllDentists:function()
{
let output = "";
for(let i = 0; i < this.dentists.length; i++)
{
output += this.dentists[i].firstName + "\n";
}
console.log(output);
console.log(this.dentists);
}
};
let dentistTest = createDentistObject("Dentist", "Dentiston", 38);
myDentalClinic.addDentist(dentistTest);
1 Answer 1
You forgot to return the created dentist object in your first function:
function createDentistObject(firstName, lastName, age)
{
let dentistObject =
{
firstName: firstName, lastName: lastName, age:age,
getFullName: function()
{
return this.firstName + " " + this.lastName;
}
};
return dentistObject;
}
Also you have some typos in your myDentalClinic object using this.dentistObject instead of dentistObject which is your argument. Fixed:
let myDentalClinic =
{
dentists:createDentistList(),
addDentist:function(dentistObject)
{
console.log("Dentistfullname: " + dentistObject.getFullName());
if(this.dentists.length == 0)
{
this.dentists.push(dentistObject);
}
else
{
/*if(this.dentistExist(dentistObject))
{
console.log("Dentist exists in the list!");
}*/
this.dentists.push(dentistObject);
}
},
dentistExist:function(dentistObject)
{
for(let i = 0; i < this.dentists.length; i++)
{
if(this.dentists[i].firstName === dentistObject.firstName)
{
return true;
}
}
return false;
},
printAllDentists:function()
{
let output = "";
for(let i = 0; i < this.dentists.length; i++)
{
output += this.dentists[i].firstName + "\n";
}
console.log(output);
console.log(this.dentists);
}
};
3 Comments
createDentistObject is invoked as function so this will either refer to the window object if JS is not in strict mode, or undefined if strict mode is set.