Changing property values with functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Changing property values with functions

Hey people! Is there a reason when I use new command, and create and object, I can use the changeName function and it works, but when I try to use it with an object I create as I did in the second bit, it doesn't work and does not output anything? var James = new person("James, 21"); James.changeName("Cliff"); document.write(James.name); var David = {name:"David", age:24}; David.changeName("Dave"); document.write(David['name']);

10th Nov 2018, 4:38 PM
Nalathol
2 Answers
+ 2
You probably have the changeName method inside the Person object. The second code should have thrown error at console that it couldn't detect changeName method. I'm assuming things as you've not posted much details on Person object
14th Nov 2018, 3:21 AM
Parkour Karthik
Parkour Karthik - avatar
0
Yes, it was within the person object. Sorry for the inconvenience of partial posting. Thanks for the answer.
14th Nov 2018, 9:40 AM
Nalathol