Duck Typing
Tags: javascript
Published: 2017-12-04 22:35:37

---
title: "Duck Typing"
published: true
tags: JavaScript,Javascript
canonical_url: http://www.bardev.com/2016/06/26/duck-typing/
---

In this post I will describe Duck Typing in regards to dynamic languages (JavaScript, PHP, Python, Ruby).

If you ever read or heard about Duck Typing, you have probably seen the following quote:

> If it walks like a duck and quacks like a duck, it must be a duck.

Without context, this quote doesn’t mean much.&nbsp; Hopefully at the end of this post the previous quote will make much more sense.

Below is an excerpt from Wikipedia.&nbsp; Without understanding Duck Typing, the following excerpt doesn’t make much sense either.

> Duck Typing requires that type checking is deferred to runtime, and is implemented by means of dynamic typing or reflection.
> 
> Duck typing is concerned with establishing the suitability of an object for some purpose. With normal typing, suitability is assumed to be determined by an object’s type only. In duck typing, an object’s suitability is determined by the presence of certain _methods and properties_ (with appropriate meaning), rather than the actual type of the object.

[https://en.wikipedia.org/wiki/Duck\_typing](https://en.wikipedia.org/wiki/Duck_typing "https://en.wikipedia.org/wiki/Duck\_typing")

### Duck Typing in JavaScript – Dynamic Language

In the following examples, we are going to create following 3 different objects in JavaScript

- point2D
- point3D&nbsp;
- me

All three objects&nbsp; will include properties: name,&nbsp; x and y, they&nbsp; also include a calculate() function.&nbsp; The Me object will include the properties occupation and birthDate. See image below for a visualization for object model.

[![image](http://www.bardev.com/wp-content/uploads/2016/06/image_thumb-14.png "image")](http://www.bardev.com/wp-content/uploads/2016/06/image-14.png)

In the code below, I created the three object (point2D, point3d, me).&nbsp; I also created a function called calculate() for each object.&nbsp; The calculate() function for all the objects returns a string.&nbsp;

There is a displayPoint() function defined.&nbsp; This function could just have been called display().&nbsp; The function displayPoint() has one parameter called “point”.&nbsp;&nbsp; The function signature looks like this:

function displayPoint(point)

The parameter name “point” doesn’t mean anything.&nbsp; I could have called it “obj”.&nbsp; I called the function “displayPoint” and the parameter “point” because contextually the function is created to work with points.&nbsp; Even though I created the function displayPoint(point) to work with points, but I can pass any type of object as a parameter in to the function.

In function displayPoint(point) the code accesses the property “name” and the method “calculate()” of the object “point”. It doesn’t matter what type of object is passed in to the function “displayPoint”, but the object must have the property “name” and the method “calculate()” to run without an error.

This is Duck Typing.

What is the “me” object? Is it a person or a point – both, neither?&nbsp; What do the properties x and y represents.&nbsp; The properties x and y could represent a point, but they could also be pointers (reference) to XY chromosomes.&nbsp; But in this context of the method “displayPoint”, the object “me”, and the properties x and y are points and treated like points.

<script src="https://gist.github.com/BarDev/e26c2b96819efb77afa3159fc217fef1.js"></script>

Now, the following quote has context. I hope it makes more sense now.

> If it walks like a duck and quacks like a duck, it must be a duck.
### Resources

- [How Duck Typing Benefits C# Developers – Phil Haack](http://haacked.com/archive/2007/08/19/why-duck-typing-matters-to-c-developers.aspx/ "How Duck Typing Benefits C# Developers - Phil Haack") 
- [Wikipedia – Duck Typing](https://en.wikipedia.org/wiki/Duck_typing "Wikipedia - Duck Typing")
- [Why TypeScript](https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html "Why TypeScript") – See Section: Types are Structural 

The post [Duck Typing](http://www.bardev.com/2016/06/26/duck-typing/) appeared first on [Mike Barlow (BarDev)](http://www.bardev.com).