Skip to content

Instantly share code, notes, and snippets.

@wavebeem
Created March 27, 2016 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavebeem/fdc3fab86b075f95266c to your computer and use it in GitHub Desktop.
Save wavebeem/fdc3fab86b075f95266c to your computer and use it in GitHub Desktop.
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