What you're trying to do is this:
console.log(rabbits[i].describeMyself());
But there's a problem here, because describeMyself()
itself logs to the console and doesn't return anything, so what you would see in the console is:
I am a fluffy rabbit
undefined
I am a happy rabbit
undefined
I am a sleepy rabbit
undefined
To remedy this, I suggest changing the definition of Rabbit
to the following:
function Rabbit(adjective) {
this.adjective = adjective;
}
Rabbit.prototype.describeMyself = function() {
return "I am a " + this.adjective + " rabbit";
};
so that it returns a string instead. Placing the function on the prototype, while a completely separate matter, should offer you some performance benefits as well since you won't be making a new copy of describeMyself
for each Rabbit
.