Dojo and Duck Typing

JavaScript is powerful yet unruly language but through creativity, standards, and best practices you can control the unruliness. Dojo has put a lot of this into consideration and the topic I am writing about today is a technique called Duck Typing.  In the book, Dojo The Definitive Guide, there is a chapter dedicated to utilities and within that chapter there is a section on type checking.

“Duck typing is based upon the saying that if it walks like a duck and quacks like a dck, then its a duck.”

Dojo comes with a bunch of methods to help with this, like isString(), isArray(), isFunction(), isObject(), isArrayLike(), isAlien().

These kind of functions can make your code a lot more safe if you expect a certain type in your function and use them to test that you received what you expected.  Unlike in Java where you can use actual interfaces and class types, you do not have that in JavaScript.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.