Objects | Sololearn: Learn to code for FREE!

+1

Objects

I have a quick question in this code: var Bradley = new person("Bradley",15,"M"); var Wesley = new person("Wesley",13,"M"); var Liberty = new person("Liberty",4,"F"); function person(name,age,gender){ this.name = name; this.age = age; this.gender = gender; } function Button() //I would have a button in the HTML side to call this function { var N = prompt("enter you name"); switch(N){ case "Bradley": document.write(Bradley); break; case "Wesley": document.write(Wesley); break; case "Liberty": document.write(Liberty); } } It outputs "[object Object]" I was wondering if I had a syntax error or if I had not defined it well enough. Any help you can give would be greatly appreciated to fix this problem :).

4/8/2018 2:19:12 PM

Bradley

5 Answers

New Answer

+2

you are writing object instance to page For example Bardley is an instance of a person class. you should use Bardley.name.

+1

OK thank you very much

+1

Your Welcome

0

Yes, but what can I do if I want to write the entire stats "Bradley ,15,M" in stead of just the name? Thank you for replying

0

then you have to write Bardley.name + ", " + Bardley.age + ", " + Bardley.gender;