I'm on JavaScript right now and a few days passed since I studied objects so I decided to try and make my own ones. The problem is that my code isn't really working. It prints me only the first objects. I'm sure that I will get good answers here because this place helped me a lot when I studied and experimented with HTML5 and CSS. Thanks a lot!
var person_1st = {
name: "Plamen",
surname: "Dobrev",
age: "14",
favourite_colour: "blue"
};
document.write(person_1st.name + "<br />" + person_1st.surname + "<br />" + person_1st.age + "<br />" + person_1st.favourite_colour);
function person_2nd(name, surname, age, favourite_colour) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_colour = favourite_colour;
this.new_favourite_colour = function(favourite_colour) {
this.favourite_color = favourite_colour;
};
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_color("red");
document.write(person_2nd_Plamen.name + "<br />" + person_2nd_Plamen.surname + "<br />" + person_2nd_Plamen.age + "<br />" + person_2nd_Plamen.favourite_colour);
function person_3rd(name, surname, age, favourite_colour) {
this.name = name;
this.surname = surname;
this.age = function(age) {
this.age = age;
};
this.favourite_colour = favourite_colour;
};
var person_3rd_Plamen = new person_3rd("Plamen", "Dobrev", 14, "blue");
person_3rd_Plamen.age(15);
document.write(person_3rd_Plamen.name + "<br />" + person_3rd_Plamen.surname + "<br />" + person_3rd_Plamen.age + "<br />" + person_3rd_Plamen.favourite_colour);
function person_4th(name, surname, age, favourite_colour) {
this.name = name;
this.surname = surname;
this.age = new_age;
this.favourite_colour = favourite_colour;
};
function new_age() {
return 15;
};
var person_4th_Plamen = new person_4th("Plamen", "Dobrev", 14, "blue");
document.write(person_4th_Plamen.name + "<br />" + person_4th_Plamen.surname + "<br />" + person_4th_Plamen.age + "<br />" + person_4th_Plamen.favourite_colour);
Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function. If you're having trouble figuring out why that is let us know but this would be a good starting pointperson_2nd_Plamen.new_favourite_coloris missing theuincolour.colorand use it consistently everywhere.