Image source: https://openclipart.org/detail/217463/jackass
// Variables
var donkey; var tail = "Tail";
// Farm Animal Constructor
function FarmAnimal(numberOfLegs) { this.getNumberOfLegs = function() { return numberOfLegs; } } FarmAnimal.prototype = { isA: function() { return "Farm Animal"; }, hasA: function(bodyPart) { return this[bodyPart] != undefined; }, }
// Donkey Constructor
function Donkey() { FarmAnimal.call(this, 4); } Donkey.prototype = Object.create(FarmAnimal.prototype); Donkey.prototype.isA = function() { return "Donkey"; } Donkey.prototype.pinTheTail = function(theTail) { this.tail = theTail; }
// Functions
function pinTailOnDonkey(d, t) { if (d != undefined) { d.pinTheTail(t) printFarmAnimal(d); } else { alert("Donkey is not defined!"); } } function printFarmAnimal(a) { console.log(a); var withTail = document.getElementById("animalWithTail"); var withoutTail = document.getElementById("animalWithoutTail"); if (a.hasA("tail")) { withTail.setAttribute("style", "display:inline"); withoutTail.setAttribute("style", "display:none"); } else { withTail.setAttribute("style", "display:none"); withoutTail.setAttribute("style", "display:inline"); } } function newDonkey() { donkey = new Donkey(); printFarmAnimal(donkey); }
// Start
newDonkey();