+ 3

Make an alert box that shows value from 2 object property.

Help needed. Only shows 1 object property which is the p1.age. The p2.age does not show. Here's the sample code: function person(name, age, color) { this.name = name; this.age = age; this.favColor = color; } var p1 = new person("John", 42, "green"); var p2 = new person("Amy", 21, "red"); alert(p1.age , p2.age );

2nd Mar 2017, 9:40 AM
Muench Riel Macarasig
Muench Riel Macarasig - avatar
4 Answers
+ 17
function person(name, age, color) { this.name = name; this.age = age; this.favColor = color; } var p1 = new person("John", 42, "green"); var p2 = new person("Amy", 21, "red"); alert([p1.age , p2.age]);
2nd Mar 2017, 9:45 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
In fact, the alert() function takes only once parameter as argument, so when you are passing your two values without brackets, you loose the second. By adding the brackets, as you noticed, you write an array litteral expression: you implicitly do: var temp = [ p1.age, p2.age ]; alert(temp); ... doing so, you pass only one parameter to the alert() function, and you don't loose your two values ;)
2nd Mar 2017, 12:44 PM
visph
visph - avatar
+ 2
So basically I need to enclose it with brackets? Like Arrays huh? Thanks a lot. I'll try it.
2nd Mar 2017, 9:47 AM
Muench Riel Macarasig
Muench Riel Macarasig - avatar