So, using Code Academy I'm in the Objects section of the Javascript tutorial and I'm having a really hard time wrapping my brain around Functions & Objects. I'd like some help and explaining with parts of the code below. I've commented on each line what I need help with. Thanks so much in advance.
// Obviously declaring the Rabbit function with adjective to be called
function Rabbit(adjective) {
// I don't understand this line, or the context of why this is being used.
this.adjective = adjective;
// Why declare another function here? Is a function within a function
// considered a Method or is that only Function within an Object?
this.describeMyself = function() {
// I get this part but why does it need a function to do this?
console.log("I am a " + this.adjective + " rabbit");
};
}
// I don't get this either, Isn't this declaring a new object? How can
// that be when you only have a Function named Rabbit?
var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");
// How could this work if describeMyself is in the Rabbit function and
// has nothing to do with rabit1?
console.log(rabbit1.describeMyself);
console.log(rabbit2.describeMyself);
console.log(rabbit3.describeMyself);
Hopefully not too confusing but if any of you more experienced Javascript people could kindly explain out everything I talked about in the comments I'd greatly appreciate it. Thanks