【Javascript】Objects / Can this get only one result? | Sololearn: Learn to code for FREE!

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.

5/25/2018 12:13:03 PM

Ayumi

6 Answers

New Answer

+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.

+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

+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

0

hi Brains. Thank you for your advice. May I ask how I insert the code? I am also a beginner on SoloLearn.. haha

0

>> Hi BlazingMagpie uhh now I got it. Thanks. :)

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. :)