Could someone give me an example of Duck Typing inheritance in Javascript? I'm exploring OO javascript and I've heard about duck typing but can't see any examples of it being used in javascript.
-
3duck typing doesn't work so well in javascript- your object might quack like a floating point number, but it may be just as likely to quack like a string or a boolean. – kennebec Jul 31 '10 at 22:32
-
1http://stackoverflow.com/a/12763070/822138 has some good examples & discussion. – sam Mar 05 '13 at 21:43
-
https://medium.com/front-end-hacking/javascript-and-duck-typing-7d0f908e2238 – M_Farahmand Jun 29 '18 at 12:03
-
See also: [What is duck typing?](https://stackoverflow.com/q/4205130/4561887). I took a quack at it [with my own answer here](https://stackoverflow.com/a/66502857/4561887). – Gabriel Staples Mar 09 '22 at 04:33
3 Answers
The rule of "Duck Typing" is
If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.
In a class-based object-oriented programming language (C++, for example) to make both objects look like a duck you must inherit their classes from a common "interface" class, so the compiler would let you call duck
methods on them. That is called a strong typing.
Now this is how it's done in Javascript:
var duck = {
appearance: "feathers",
quack: function duck_quack(what) {
print(what + " quack-quack!");
},
color: "black"
};
var someAnimal = {
appearance: "feathers",
quack: function animal_quack(what) {
print(what + " whoof-whoof!");
},
eyes: "yellow"
};
function check(who) {
if ((who.appearance == "feathers") && (typeof who.quack == "function")) {
who.quack("I look like a duck!\n");
return true;
}
return false;
}
check(duck); // true
check(someAnimal); // true
See, the check
function check whether the passed object looks like a duck (it checks appearance and its' ability to quack). We pass two different objects to it and it will return true
on both. Besides the appearance and quacking these may be completely different things, but IN THIS PARTICULAR check
function they behave the same way (have a common interface), they both look like a "duck". We can call the quack
method on both objects (and who cares what they really are).

- 35,493
- 19
- 190
- 259

- 1,491
- 2
- 8
- 3
-
5This is definitely the best example here. It follows the original example of "duck typing": In other words, don’t check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with. (from: http://haacked.com/archive/2014/01/04/duck-typing/) – lintuxvi Jan 10 '17 at 19:25
-
1Great answer, I was banging my head, its simple now in order to make your variable as object like another or object, your variable should have all properties of another object or object. So only we can call it as object... So if it looks like duck, swims like duck then its duck hehe nice one. – Sudarshan Kalebere Feb 27 '17 at 19:23
-
The second link gives an example of a duck-typing-like pattern in js. Not saying I recommend doing this, but... well, you asked for it. ;)
In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.
The simplest approach is to define the contract informally and simply rely on the developers at each side of the interface to know what they are doing. Dave Thomas has given this approach the name of "duck typing" —if it walks like a duck and it quacks like a duck, then it is a duck. Similarly with our
Shape
interface, if it can compute an area and a perimeter, then it is aShape
.

- 35,493
- 19
- 190
- 259

- 75,346
- 19
- 113
- 141
-
Brilliant, thanks for the replies. Now understanding I think i'm going to stick to my standard inheritance using the prototype chain. – Mike Rifgin Aug 01 '10 at 22:10
-
1The js example in wikipedia, does not check for any "walks" and "quacks"!! Shouldn't that example be updated? – eminemence Sep 07 '12 at 18:46
An example for duck typing in JavaScript are iterables. JavaScript has actually no type called Iterable
, but it has a definition for an object, which is iterable.
In order to be iterable, an object must implement the @@iterator method
This is the requirement for duck typing. If an object implements a method, defined in an interface, the object can be used in places, where the interface type is accepted. For iterables this is the case in loops
for (let value of iterable) {
}
and arrays
[...iterable]

- 21,900
- 13
- 104
- 178