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

8th Apr 2018, 2:19 PM
Bradley
5 Respostas
+ 2
you are writing object instance to page For example Bardley is an instance of a person class. you should use Bardley.name.
8th Apr 2018, 2:46 PM
Rizvan Kukad
+ 1
OK thank you very much
8th Apr 2018, 3:00 PM
Bradley
+ 1
Your Welcome
8th Apr 2018, 3:01 PM
Rizvan Kukad
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
8th Apr 2018, 2:54 PM
Bradley
0
then you have to write Bardley.name + ", " + Bardley.age + ", " + Bardley.gender;
8th Apr 2018, 2:58 PM
Rizvan Kukad