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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s