Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function fly(creature) {
return (
"The " + creature.species +
" flies using its " + creature.methodOfFlight
);
}
function hunt(creature, otherCreature) {
return (
"The " + creature.species +
" hunts the " + otherCreature.species +
" using its " + creature.methodOfHunting
);
}
function CreateCreature(species) {
return {species};
}
function CreatePredator(species, methodOfHunting) {
const creature = {species, methodOfHunting};
creature.hunt = hunt.bind(null, creature);
return creature;
}
function CreateFlyingPredator(species, methodOfFlight, methodOfHunting) {
const creature = {species, methodOfFlight, methodOfHunting};
creature.fly = fly.bind(null, creature);
creature.hunt = hunt.bind(null, creature);
return creature;
}
const rabbit = CreatePredator("rabbit");
const wolf = CreatePredator("wolf", "teeth");
const eagle = CreateFlyingPredator("eagle", "wings", "talons");
console.log(rabbit);
console.log(eagle);
console.log(wolf);
console.log(wolf.hunt(rabbit));
console.log(eagle.hunt(wolf));
console.log(eagle.fly());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment