Skip to content

Instantly share code, notes, and snippets.

@schnogz
Created May 17, 2015 18:10
Show Gist options
  • Save schnogz/213a08f61f6d7d8cc5d3 to your computer and use it in GitHub Desktop.
Save schnogz/213a08f61f6d7d8cc5d3 to your computer and use it in GitHub Desktop.
JavaScript Duck Typing Example
function Animal(hasFeathers, sound) {
this.hasFeathers = hasFeathers;
this.sound = sound;
}
Animal.prototype.isDuck = function() {
if (this.hasFeathers && this.sound === "Quack") {
console.log("I am a duck");
} else {
console.log("I am not a duck");
}
}
var duck = new Animal(true, "Quack");
var turkey = new Animal(true, "Gobble");
duck.isDuck(); // true
turkey.isDuck(); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment