0

【Javascript】Objects / Can this get only one result?

Hi guys. I am a Javascript beginner and having a difficulty understanding Objects. I was practicing this below and just wondered why only one result can be shown. The result that is shown on my browser is only the fullName one. document.getElementById("test").innerHTML = person["firstName"]; document.getElementById("test").innerHTML = person.firstName; document.getElementById("test").innerHTML = person.fullName(); The reason why I typed these similar document tags above was just to understand what kind of ways exist and to figure out which is the easiest one for me. I would appreciate it if someone can help me on this. Here is the complete tags I made. <p id="test"></p> <script> var person = { firstName:"Michael", lastName:"Smith", age:26, fullName:function() { return this.firstName + " " + this.lastName; } }; document.getElementById("test").innerHTML = person["firstName"]; document.getElementById("test").innerHTML = person.firstName; document.getElementById("test").innerHTML = person.fullName(); </script> Thanks.

25th May 2018, 12:13 PM
Ayumi
Ayumi  - avatar
6 Answers
+ 1
That's because you're setting everything to same element. At first you set firstName, then override it with lastName, then override it with fullName.
25th May 2018, 12:18 PM
BlazingMagpie
BlazingMagpie - avatar
+ 3
Ayumi as much as illd love to help,i cannot interprete your code as a post,insert as a code so we can run it,and then you might get help
25th May 2018, 12:15 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
whilst editing your post youlld see a "+" sign,tap it and youll see: "insert code",tap it and as BlazingMagpie said youe only resetting the innerHTML of text. its like:: var a=0; a=1; a=2; the final value of a will be 2 not that 012
25th May 2018, 12:23 PM
᠌᠌Code X
᠌᠌Code X - avatar
0
hi Brains. Thank you for your advice. May I ask how I insert the code? I am also a beginner on SoloLearn.. haha
25th May 2018, 12:19 PM
Ayumi
Ayumi  - avatar
0
>> Hi BlazingMagpie uhh now I got it. Thanks. :)
25th May 2018, 12:25 PM
Ayumi
Ayumi  - avatar
0
>> Brains Uh I see what you mean. I am posting from my laptop. I checked the mobile app now and I see the plus button for adding codes. Thank you for your help. It was very helpful. :)
25th May 2018, 12:48 PM
Ayumi
Ayumi  - avatar