I wouldn't say using classes is the conventional way to accomplish stuff in Javascript, but it can work, nonetheless. What you need to do is break the problem down into smaller parts. Creating the person class should come first (pretty straightforward):
Code:
function Person() {}
Then you should add your prototypes. You should have a prototype for each class variable and for each variable you should have an accessor function to return that value:
Code:
function Person() {}
Person.prototype._age;
Person.prototype._numKids;
Person.prototype.age = function() { return this._age; }
Person.prototype.numKids = function() { return this._numKids; }
Then you should create some sort of variable initialisation in the constructor of your class. Ideally in your case, this will create an age and a number of kids (if any):
Code:
function Person() {
this._age = Math.random()*30+20;
this._numKids = <span style="color:red;">(Math.random() < 0.5)</span>?<span style="color:blue;">Math.random()*5+1</span>:<span style="color:green;">0</span>;
// For reference, this is a shorthand IF expression is of the form: <span style="color:red;">(condition)</span>?<span style="color:blue;">value to return if condition is true</span>:<span style="color:green;">value to return if condition is false</span>;
// In other words, numKids = random number from 1-5 if condition < 0.5, else numKids = 0. Math.random() always returns a number between 0 and 1, so there's a 50/50 chance of it evaluating to less than 0.5, hence it being your condition.
}
Person.prototype._age;
Person.prototype._numKids;
Person.prototype.age = function() { return this._age; }
Person.prototype.numKids = function() { return this._numKids; }
This should get you started. Other things to consider:
<ul><li>Creating the children as People objects in the constructor of Person.</li>
<li>Creating a function for the Person class that increases the age of the Person by 1.</li>
<li>I'm not really sure what the point of aging them to 100 is, so whilst reading my code bear in mind that there might be something there that you need to think about.</li>
<li>Initialising the collection of 100 Person objects. You should use an array for this, eg.
var personArray = new Array();
for (var i = 0; i < 100; i++) personArray[i] = new Person();</li>
</ul>