Javascript using constructor inside array

I have code like this, then I was confused about how to quote a family of arrays to print each member under a person.

function Person(name,age){ this.name = name; this.age = age; } var family = []; family[0] = new Person("alice",40); family[1] = new Person("bob",42); family[2] = new Person("michelle",8); family[3] = new Person("timmy",6); 
+5
source share
1 answer

here is jsfiddle

- This is what you need?

 for (var key in family) { var obj = family[key]; for (var prop in obj) { alert(prop + " = " + obj[prop]); } } 

Here is a way to access the properties directly, not with the jsFIddle loop (method 2, uncomment)

+2
source

Source: https://habr.com/ru/post/1214172/


All Articles