Introduction to Objects 25: Rabbit constructor


#1

Have passed this one, but the output is a little confusing so just wanted to check to see what i'm doing wrong:

function Rabbit(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}

// now we can easily make all of our rabbits

var rabbit1 = new Rabbit ("fluffy")
var rabbit2 = new Rabbit ("happy")
var rabbit3 = new Rabbit ("sleepy")

console.log(rabbit1.describeMyself());
console.log(rabbit2.describeMyself());
console.log(rabbit3.describeMyself());

I've tried taking out console.log from the last bit where i'm calling the method but then it doesn't work at all.

This is my output:

I am a fluffy rabbit
undefined
I am a happy rabbit
undefined
I am a sleepy rabbit
undefined

It does accept it and say i've finished the section, but what's causing the undefined??


#2

@nataliemp,

As you are already using a console.log() in your descibeMyself Method
you only have to make a call on this describeMyself() Method,
otherwise you will experience the phenomen

I am a fluffy rabbit
undefined

function Rabbit(adjective) {
   this.adjective = adjective;
   this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
        };
}

// now we can easily make all of our rabbits

var rabbit1 = new Rabbit ("fluffy");
console.log(rabbit1);
console.log("== getting definition of rabbit1.describeMyself ==");
console.log("== using Function.prototype.toString() method   ==");
console.log(rabbit1.describeMyself.toString());
console.log("=========================================");
var rabbit2 = new Rabbit ("happy");
console.log(rabbit2);
console.log("=========================================");
var rabbit3 = new Rabbit ("sleepy");
console.log(rabbit3);
console.log("=========================================");

rabbit1.describeMyself();
rabbit2.describeMyself();
rabbit3.describeMyself();

would give the [output]

{ adjective: 'fluffy', describeMyself: [Function] }
== getting definition of rabbit1.describeMyself ==
== using Function.prototype.toString() method   ==
function() {
        console.log("I am a " + this.adjective + " rabbit");
        }
=========================================
{ adjective: 'happy', describeMyself: [Function] }
=========================================
{ adjective: 'sleepy', describeMyself: [Function] }
=========================================
I am a fluffy rabbit
I am a happy rabbit
I am a sleepy rabbit

#3

Thanks for that. I thought it was to do with the use of console.log in the initial method, but then it wasn't working at all, I'd missed the semicolons after declaring my new rabbits though so maybe that was it!