25. Constructors in Review


#1


what is wrong?

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


#2

Your code is correct just add two lines

first line -> make a rabbit object from the above rabbit1 function
second line -> access the function describeMyself with that object

here is the code

function rabbit1(adjective) {
	this.adjective = "fluffy";		
        this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}
		
var rabbit = new rabbit1();
rabbit.describeMyself();

#3

still gives me the same massage which is
Oops, try again. Make sure to give rabbit1 the adjective fluffy

here is the full code

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

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

var rabbit = new rabbit1();
rabbit.describeMyself();
function rabbit2(adjective) {
this.adjective = "happy";
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}
var rabbit = new rabbit2();
rabbit.describeMyself();
function rabbit3(adjective) {
this.adjective = "sleepy";
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit");
};
}
var rabbit = new rabbit3();
rabbit.describeMyself();


#4

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");{
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit1");
};
}

var rabbit2 = new Rabbit("happy");{
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit2");
};
}

var rabbit3 = new Rabbit("sleepy");{
this.describeMyself = function() {
console.log("I am a " + this.adjective + " rabbit3");
};
}


#5

Try this...we make a single function and make multiple objects and a single console.log statement to make things very short and handy

               function Rabbit(adjective) {
			this.adjective = adjective;
			this.describeMyself = function() {
			console.log("I am a " + this.adjective + " rabbit");
			};
		}
		
		var rabbit1 = new Rabbit("fluffy");
		var rabbit2 = new Rabbit("happy");
		var rabbit3 = new Rabbit("sleepy");
		rabbit1.describeMyself();
		rabbit2.describeMyself();
		rabbit3.describeMyself();

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.