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.
TypeScript uses the duck-typing method to compare one object with other objects by checking that both objects have the same type matching names or not. It means we cannot change the signature of a variable.
Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose.
Depending on the language, variables' type can be defined statically or dynamically. Ruby relies on a principle that is called Duck Typing.
Duck Typing is a term commonly related to dynamically typed programming languages and polymorphism. The idea behind this principle is that the code itself does not care about whether an object is a duck, but instead it does only care about whether it quacks.
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).
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.
Wikipedia - Duck typing
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
.
JavaScript Interfaces and Duck Typing
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With