25. Constructors in Review


#1

The following is my code:

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 = Rabbit("fluffy");
var rabbit2 = Rabbit("happy");
var rabbit3 = Rabbit("sleepy");

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

I keep getting "Oops, try again. When checking rabbit1, your code caused the following error: TypeError: Cannot read property 'describeMyself' of undefined".

What am I doing wrong?


#2

Hi ist missing the word new before Rabbit() like that

var rabbit1 = new Rabbit("fluffy");

#3

Olá.
Também tente este:

function Rabbit(adjective) {
this.adjective = adjective;
this.describeMyself = function() {
console.log("Eu sou um " + this.adjective + " coelho");
};
}

// agora podemos fazer facilmente todos os nossos coelhos
var rabbit1 = new Rabbit("fofinho");
var rabbit2 = new Rabbit("alegre");
var rabbit3 = new Rabbit("sonolento");

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