Eloquent JavaScript - chapter 06 - objects

by Denise Nepraunig

JavaScript

var rabbit = {};

rabbit.speak = function() {
    console.log("I am a rabbit");  
};

rabbit.speak();

function rabbitSpeak(line) {
    console.log(this.type + " says: ");
    console.log(line);
}

var fatRabbit = {
    type : "fat",
    speak : rabbitSpeak
};

var whiteRabbit = {
    type : "white",
    speak : rabbitSpeak
}

fatRabbit.speak("Hy I am fatty!");
whiteRabbit.speak("Hy I am whity");

function Rabbit(type) {
    this.type = type;
}

Rabbit.prototype.speak = function (line) {
    console.log(this.type + " says: ");
    console.log(line);
};

var blackBunny = new Rabbit("black");
blackBunny.speak("This is le me - le Bunny the black");