My JavaScript code with objects and methods isn't working
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);
javascript function object methods
add a comment |
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);
javascript function object methods
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw: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 point
– aug
Nov 15 '18 at 23:09
You have a typo:person_2nd_Plamen.new_favourite_color
is missing theu
incolour
.
– Barmar
Nov 15 '18 at 23:13
You should pick either American or British spelling ofcolor
and use it consistently everywhere.
– Barmar
Nov 15 '18 at 23:14
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31
add a comment |
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);
javascript function object methods
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);
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);
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);
javascript function object methods
javascript function object methods
edited Nov 15 '18 at 23:30
Temani Afif
79.5k94692
79.5k94692
asked Nov 15 '18 at 23:04
Plamen DobrevPlamen Dobrev
31
31
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw: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 point
– aug
Nov 15 '18 at 23:09
You have a typo:person_2nd_Plamen.new_favourite_color
is missing theu
incolour
.
– Barmar
Nov 15 '18 at 23:13
You should pick either American or British spelling ofcolor
and use it consistently everywhere.
– Barmar
Nov 15 '18 at 23:14
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31
add a comment |
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw: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 point
– aug
Nov 15 '18 at 23:09
You have a typo:person_2nd_Plamen.new_favourite_color
is missing theu
incolour
.
– Barmar
Nov 15 '18 at 23:13
You should pick either American or British spelling ofcolor
and use it consistently everywhere.
– Barmar
Nov 15 '18 at 23:14
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw:
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 point– aug
Nov 15 '18 at 23:09
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw:
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 point– aug
Nov 15 '18 at 23:09
You have a typo:
person_2nd_Plamen.new_favourite_color
is missing the u
in colour
.– Barmar
Nov 15 '18 at 23:13
You have a typo:
person_2nd_Plamen.new_favourite_color
is missing the u
in colour
.– Barmar
Nov 15 '18 at 23:13
You should pick either American or British spelling of
color
and use it consistently everywhere.– Barmar
Nov 15 '18 at 23:14
You should pick either American or British spelling of
color
and use it consistently everywhere.– Barmar
Nov 15 '18 at 23:14
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31
add a comment |
3 Answers
3
active
oldest
votes
It's great that you're interested in learning Javascript!
As @aug mentioned in the comments, the browser-console is a great tool to figure out whats going wrong. In this case you are getting a type error: Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function
In this case you had spelled color
differently in different places.
Some times you spelled it color
but other times 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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
add a comment |
Try something like this for understanding:
function Person() {
this.name;
this.surname;
this.age;
this.favourite_colour;
this.assign = function(n, s, a, fc) {
this.name = n;
this.surname = s;
this.age = a;
this.favourite_colour = fc;
}
}
var p = new Person(); // create object
p.assign("Plamen", "Dobrev", "18", "blue")
alert(p.name);
var p = ;
p.push(new Person());
p[0].assign("Plamen", "Dobrev", "18", "blue"); // first index
alert(p[0].age); // get age of first index for example
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
add a comment |
Plamen. Glad you're taking an interest in web development and JavaScript, hope you learn a lot.
As pointed out by a few members you mis-spelled the word `color - hit F12 on your browser to open the console and you'll get useful hints when there's an error. You can also write data directly to the console from your JavaScript code:
console.log("Hi, Plamen!");
There's some really cool things we can do in JavaScript, your code is great but we could tidy it up and make it cooler. The great thing about our object function is that we really only need to have one of them. We can use it to create multiple instances of the object, you could have a single person
function, like this:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
}
And use it to create lot's of people, every person you create from this object function will inherit the properties and methods of the object function.
person_1 = new person("My name", "My surname", 38, "blue");
person_2 = new person("Your name", "Your surname", 123, "magenta");
person_3 = new person("Someone", "else", 4321, "purple");
document.write("Person 1 name: " + person_1.name + " " + person_1.surname);
document.write("Person 2 name: " + person_2.name + " " + person_2.surname);
document.write("Person 3 name: " + person_3.name + " " + person_3.surname);
// Or, view output in console
console.log("Person 1 name: " + person_1.name + " " + person_1.surname);
console.log("Person 2 name: " + person_2.name + " " + person_2.surname);
console.log("Person 3 name: " + person_3.name + " " + person_3.surname);
Think of the function as a sort of template that can be used to create many instances of the object. Your instances can inherit methods as well as properties:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
this.changeFavouriteColor = function(color) {
this.favourite_color = color;
};
this.changeAge = function(age) {
this.age = age;
};
};
person_1 = new person("me", "misturr", 20, "orange");
person_2 = new person("him", "surrr", 50, "yellow");
person_1.changeAge(21);
person_2.changeFavouriteColor("green");
console.log(`${person_1.name} is ${person_1.age} years old`);
console.log(`${person_2.name}'s favourite color is ${person_2.favourite_color}`);
JavaScript is pretty cool, hope you have fun and learn more.
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329113%2fmy-javascript-code-with-objects-and-methods-isnt-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's great that you're interested in learning Javascript!
As @aug mentioned in the comments, the browser-console is a great tool to figure out whats going wrong. In this case you are getting a type error: Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function
In this case you had spelled color
differently in different places.
Some times you spelled it color
but other times 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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
add a comment |
It's great that you're interested in learning Javascript!
As @aug mentioned in the comments, the browser-console is a great tool to figure out whats going wrong. In this case you are getting a type error: Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function
In this case you had spelled color
differently in different places.
Some times you spelled it color
but other times 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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
add a comment |
It's great that you're interested in learning Javascript!
As @aug mentioned in the comments, the browser-console is a great tool to figure out whats going wrong. In this case you are getting a type error: Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function
In this case you had spelled color
differently in different places.
Some times you spelled it color
but other times 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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
It's great that you're interested in learning Javascript!
As @aug mentioned in the comments, the browser-console is a great tool to figure out whats going wrong. In this case you are getting a type error: Uncaught TypeError: person_2nd_Plamen.new_favourite_color is not a function
In this case you had spelled color
differently in different places.
Some times you spelled it color
but other times 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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
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_colour = favourite_colour;
}
};
var person_2nd_Plamen = new person_2nd("Plamen", "Dobrev", 14, "blue");
person_2nd_Plamen.new_favourite_colour("red");
document.write(
person_2nd_Plamen.name + "<br />" +
person_2nd_Plamen.surname + "<br />" +
person_2nd_Plamen.age + "<br />" +
person_2nd_Plamen.favourite_colour
);
console.log(person_2nd_Plamen);
edited Nov 15 '18 at 23:27
answered Nov 15 '18 at 23:15
Olian04Olian04
2,16511136
2,16511136
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
add a comment |
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
1
1
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
Thank you for the fast and helpful response and advice. I don't speak English natively, so that's probably why I did this mistake. I'll try to be more careful next time, thanks a lot!
– Plamen Dobrev
Nov 15 '18 at 23:33
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
@PlamenDobrev No problem at all. If you feel that my answer solved your problem, then feel free to click on the checkmark next to my answer.
– Olian04
Nov 15 '18 at 23:35
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
I did, it's well deserved!
– Plamen Dobrev
Nov 15 '18 at 23:36
add a comment |
Try something like this for understanding:
function Person() {
this.name;
this.surname;
this.age;
this.favourite_colour;
this.assign = function(n, s, a, fc) {
this.name = n;
this.surname = s;
this.age = a;
this.favourite_colour = fc;
}
}
var p = new Person(); // create object
p.assign("Plamen", "Dobrev", "18", "blue")
alert(p.name);
var p = ;
p.push(new Person());
p[0].assign("Plamen", "Dobrev", "18", "blue"); // first index
alert(p[0].age); // get age of first index for example
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
add a comment |
Try something like this for understanding:
function Person() {
this.name;
this.surname;
this.age;
this.favourite_colour;
this.assign = function(n, s, a, fc) {
this.name = n;
this.surname = s;
this.age = a;
this.favourite_colour = fc;
}
}
var p = new Person(); // create object
p.assign("Plamen", "Dobrev", "18", "blue")
alert(p.name);
var p = ;
p.push(new Person());
p[0].assign("Plamen", "Dobrev", "18", "blue"); // first index
alert(p[0].age); // get age of first index for example
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
add a comment |
Try something like this for understanding:
function Person() {
this.name;
this.surname;
this.age;
this.favourite_colour;
this.assign = function(n, s, a, fc) {
this.name = n;
this.surname = s;
this.age = a;
this.favourite_colour = fc;
}
}
var p = new Person(); // create object
p.assign("Plamen", "Dobrev", "18", "blue")
alert(p.name);
var p = ;
p.push(new Person());
p[0].assign("Plamen", "Dobrev", "18", "blue"); // first index
alert(p[0].age); // get age of first index for example
Try something like this for understanding:
function Person() {
this.name;
this.surname;
this.age;
this.favourite_colour;
this.assign = function(n, s, a, fc) {
this.name = n;
this.surname = s;
this.age = a;
this.favourite_colour = fc;
}
}
var p = new Person(); // create object
p.assign("Plamen", "Dobrev", "18", "blue")
alert(p.name);
var p = ;
p.push(new Person());
p[0].assign("Plamen", "Dobrev", "18", "blue"); // first index
alert(p[0].age); // get age of first index for example
answered Nov 15 '18 at 23:25
JCssJCss
347
347
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
add a comment |
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
Thanks for the code, I still can't seem to understand the last part of it, hopefully I will be able to in the future.
– Plamen Dobrev
Nov 15 '18 at 23:35
add a comment |
Plamen. Glad you're taking an interest in web development and JavaScript, hope you learn a lot.
As pointed out by a few members you mis-spelled the word `color - hit F12 on your browser to open the console and you'll get useful hints when there's an error. You can also write data directly to the console from your JavaScript code:
console.log("Hi, Plamen!");
There's some really cool things we can do in JavaScript, your code is great but we could tidy it up and make it cooler. The great thing about our object function is that we really only need to have one of them. We can use it to create multiple instances of the object, you could have a single person
function, like this:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
}
And use it to create lot's of people, every person you create from this object function will inherit the properties and methods of the object function.
person_1 = new person("My name", "My surname", 38, "blue");
person_2 = new person("Your name", "Your surname", 123, "magenta");
person_3 = new person("Someone", "else", 4321, "purple");
document.write("Person 1 name: " + person_1.name + " " + person_1.surname);
document.write("Person 2 name: " + person_2.name + " " + person_2.surname);
document.write("Person 3 name: " + person_3.name + " " + person_3.surname);
// Or, view output in console
console.log("Person 1 name: " + person_1.name + " " + person_1.surname);
console.log("Person 2 name: " + person_2.name + " " + person_2.surname);
console.log("Person 3 name: " + person_3.name + " " + person_3.surname);
Think of the function as a sort of template that can be used to create many instances of the object. Your instances can inherit methods as well as properties:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
this.changeFavouriteColor = function(color) {
this.favourite_color = color;
};
this.changeAge = function(age) {
this.age = age;
};
};
person_1 = new person("me", "misturr", 20, "orange");
person_2 = new person("him", "surrr", 50, "yellow");
person_1.changeAge(21);
person_2.changeFavouriteColor("green");
console.log(`${person_1.name} is ${person_1.age} years old`);
console.log(`${person_2.name}'s favourite color is ${person_2.favourite_color}`);
JavaScript is pretty cool, hope you have fun and learn more.
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
add a comment |
Plamen. Glad you're taking an interest in web development and JavaScript, hope you learn a lot.
As pointed out by a few members you mis-spelled the word `color - hit F12 on your browser to open the console and you'll get useful hints when there's an error. You can also write data directly to the console from your JavaScript code:
console.log("Hi, Plamen!");
There's some really cool things we can do in JavaScript, your code is great but we could tidy it up and make it cooler. The great thing about our object function is that we really only need to have one of them. We can use it to create multiple instances of the object, you could have a single person
function, like this:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
}
And use it to create lot's of people, every person you create from this object function will inherit the properties and methods of the object function.
person_1 = new person("My name", "My surname", 38, "blue");
person_2 = new person("Your name", "Your surname", 123, "magenta");
person_3 = new person("Someone", "else", 4321, "purple");
document.write("Person 1 name: " + person_1.name + " " + person_1.surname);
document.write("Person 2 name: " + person_2.name + " " + person_2.surname);
document.write("Person 3 name: " + person_3.name + " " + person_3.surname);
// Or, view output in console
console.log("Person 1 name: " + person_1.name + " " + person_1.surname);
console.log("Person 2 name: " + person_2.name + " " + person_2.surname);
console.log("Person 3 name: " + person_3.name + " " + person_3.surname);
Think of the function as a sort of template that can be used to create many instances of the object. Your instances can inherit methods as well as properties:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
this.changeFavouriteColor = function(color) {
this.favourite_color = color;
};
this.changeAge = function(age) {
this.age = age;
};
};
person_1 = new person("me", "misturr", 20, "orange");
person_2 = new person("him", "surrr", 50, "yellow");
person_1.changeAge(21);
person_2.changeFavouriteColor("green");
console.log(`${person_1.name} is ${person_1.age} years old`);
console.log(`${person_2.name}'s favourite color is ${person_2.favourite_color}`);
JavaScript is pretty cool, hope you have fun and learn more.
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
add a comment |
Plamen. Glad you're taking an interest in web development and JavaScript, hope you learn a lot.
As pointed out by a few members you mis-spelled the word `color - hit F12 on your browser to open the console and you'll get useful hints when there's an error. You can also write data directly to the console from your JavaScript code:
console.log("Hi, Plamen!");
There's some really cool things we can do in JavaScript, your code is great but we could tidy it up and make it cooler. The great thing about our object function is that we really only need to have one of them. We can use it to create multiple instances of the object, you could have a single person
function, like this:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
}
And use it to create lot's of people, every person you create from this object function will inherit the properties and methods of the object function.
person_1 = new person("My name", "My surname", 38, "blue");
person_2 = new person("Your name", "Your surname", 123, "magenta");
person_3 = new person("Someone", "else", 4321, "purple");
document.write("Person 1 name: " + person_1.name + " " + person_1.surname);
document.write("Person 2 name: " + person_2.name + " " + person_2.surname);
document.write("Person 3 name: " + person_3.name + " " + person_3.surname);
// Or, view output in console
console.log("Person 1 name: " + person_1.name + " " + person_1.surname);
console.log("Person 2 name: " + person_2.name + " " + person_2.surname);
console.log("Person 3 name: " + person_3.name + " " + person_3.surname);
Think of the function as a sort of template that can be used to create many instances of the object. Your instances can inherit methods as well as properties:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
this.changeFavouriteColor = function(color) {
this.favourite_color = color;
};
this.changeAge = function(age) {
this.age = age;
};
};
person_1 = new person("me", "misturr", 20, "orange");
person_2 = new person("him", "surrr", 50, "yellow");
person_1.changeAge(21);
person_2.changeFavouriteColor("green");
console.log(`${person_1.name} is ${person_1.age} years old`);
console.log(`${person_2.name}'s favourite color is ${person_2.favourite_color}`);
JavaScript is pretty cool, hope you have fun and learn more.
Plamen. Glad you're taking an interest in web development and JavaScript, hope you learn a lot.
As pointed out by a few members you mis-spelled the word `color - hit F12 on your browser to open the console and you'll get useful hints when there's an error. You can also write data directly to the console from your JavaScript code:
console.log("Hi, Plamen!");
There's some really cool things we can do in JavaScript, your code is great but we could tidy it up and make it cooler. The great thing about our object function is that we really only need to have one of them. We can use it to create multiple instances of the object, you could have a single person
function, like this:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
}
And use it to create lot's of people, every person you create from this object function will inherit the properties and methods of the object function.
person_1 = new person("My name", "My surname", 38, "blue");
person_2 = new person("Your name", "Your surname", 123, "magenta");
person_3 = new person("Someone", "else", 4321, "purple");
document.write("Person 1 name: " + person_1.name + " " + person_1.surname);
document.write("Person 2 name: " + person_2.name + " " + person_2.surname);
document.write("Person 3 name: " + person_3.name + " " + person_3.surname);
// Or, view output in console
console.log("Person 1 name: " + person_1.name + " " + person_1.surname);
console.log("Person 2 name: " + person_2.name + " " + person_2.surname);
console.log("Person 3 name: " + person_3.name + " " + person_3.surname);
Think of the function as a sort of template that can be used to create many instances of the object. Your instances can inherit methods as well as properties:
function person(name, surname, age, favourite_color) {
this.name = name;
this.surname = surname;
this.age = age;
this.favourite_color = favourite_color;
this.changeFavouriteColor = function(color) {
this.favourite_color = color;
};
this.changeAge = function(age) {
this.age = age;
};
};
person_1 = new person("me", "misturr", 20, "orange");
person_2 = new person("him", "surrr", 50, "yellow");
person_1.changeAge(21);
person_2.changeFavouriteColor("green");
console.log(`${person_1.name} is ${person_1.age} years old`);
console.log(`${person_2.name}'s favourite color is ${person_2.favourite_color}`);
JavaScript is pretty cool, hope you have fun and learn more.
answered Nov 15 '18 at 23:39
NunchyNunchy
825411
825411
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
add a comment |
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
1
1
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
Thanks for the great and helpful advices! Really appreciate the time you spend on writing them, along with the codes, I understood anything.
– Plamen Dobrev
Nov 16 '18 at 7:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329113%2fmy-javascript-code-with-objects-and-methods-isnt-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hey @PlamenDobrev glad you're taking an interest in JavaScript! It looks like there might be some console errors. Specifically here's one I saw:
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 point– aug
Nov 15 '18 at 23:09
You have a typo:
person_2nd_Plamen.new_favourite_color
is missing theu
incolour
.– Barmar
Nov 15 '18 at 23:13
You should pick either American or British spelling of
color
and use it consistently everywhere.– Barmar
Nov 15 '18 at 23:14
Thank you both for the fast and good responses! The reason I probably wrote that word different on some places is because I don't speak English natively, I'll try to be more careful next time. Thanks!
– Plamen Dobrev
Nov 15 '18 at 23:31