Skip to content

Instantly share code, notes, and snippets.

@bethrobson
Last active August 29, 2015 14:25
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 bethrobson/580a9fb80f690b23afc4 to your computer and use it in GitHub Desktop.
Save bethrobson/580a9fb80f690b23afc4 to your computer and use it in GitHub Desktop.
Code for Dogs and ShowDogs using class and extends (from Head First JavaScript Programming)
<!doctype html>
<html>
<head>
<title>Prototypes and Classes</title>
<meta charset="utf-8">
<script src="https://traceur-compiler.googlecode.com/git/bin/traceur.js"
type="text/javascript"></script>
<script src="https://traceur-compiler.googlecode.com/git/src/bootstrap.js"
type="text/javascript"></script>
<script>
traceur.options.experimental = true;
</script>
<script type="module">
class Dog {
constructor(name, breed, weight) {
this.name = name;
this.breed = breed;
this.weight = weight;
};
bark() {
console.log(this.name + " says woof woof!");
};
run() {
console.log(this.name + " is running");
};
wag() {
console.log(this.name + " is wagging");
};
toString() {
return "DOG: " + this.name + " weighs " + this.weight + " pounds.";
};
static describe() {
console.log("All dogs are canines");
};
};
class ShowDog extends Dog {
constructor(name, breed, weight, handler) {
super(name, breed, weight);
this.handler = handler;
};
// prototype methods
stack() {
console.log(this.name + " is stacking");
};
bait() {
console.log(this.name + " is eating a bait");
};
gait() {
console.log(this.name + " is gaiting");
};
groom() {
console.log(this.name + " is being groomed");
};
toString() {
return "SHOW" + super.toString();
};
}
var fido = new Dog("Fido", "Mixed", 24);
var scotty = new ShowDog("Scotty", "Scottish Terrier", 15, "Cookie");
var spot = new ShowDog("Spot", "Poodle", 34, "Beth");
ShowDog.describe();
console.log(scotty.toString());
</script>
</head>
<body>
<h1>Dogs and Show Dogs</h1>
<div id="output"><div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment