Rabbit object constructor with nested function

EDIT: no longer private, because the notes are good and I thought back to them this morning, so, good enough for me! This is from Codecademy Introduction to Objects 1 25/33.
Private because a) it’s not my code and b) there’s no commentary but it’s important enough to log

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");

rabbit1.describeMyself(); // called with constructed object, NOT constructor name.
rabbit2.describeMyself(); // constructedObject.internalMethod
rabbit3.describeMyself(); // output: "I am a sleepy rabbit!"
Advertisement