Skip to content

Instantly share code, notes, and snippets.

@erikfenriz
Last active March 19, 2018 17:30
Show Gist options
  • Save erikfenriz/4fe2beebf87cb09fabfdce6de0761362 to your computer and use it in GitHub Desktop.
Save erikfenriz/4fe2beebf87cb09fabfdce6de0761362 to your computer and use it in GitHub Desktop.
Animals
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function Animal() {};
Animal.prototype.run = function() {
console.log("Animals run");
};
Animal.prototype.sleep = function() {
console.log("Animals sleep");
};
Animal.prototype.eat = function() {
console.log("Animals eat");
console.log(this);
};
function Predator() {};
Predator.prototype = Object.create(Animal.prototype);
Predator.prototype.constructor = Predator;
Predator.prototype.eat = function() {
if(this == dog){console.log("I'm useful only on the hunt ")}
if(this == cat){
console.log("I'd only be capable of eating a small mouse");
}
};
function Vegan() {};
Vegan.prototype = Object.create(Animal.prototype);
Vegan.prototype.constructor = Vegan;
Vegan.prototype.eat = function() {
console.log("I eat grass!");
document.body.textContent = "I eat grass!"
};
Predator.eat = function(){}
Vegan.eat = function(){}
let dog = new Predator();
let cat = new Predator();
let mouse = new Vegan();
let cow = new Vegan();
dog.eat();
cat.eat();
mouse.eat();
cow.eat();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment