In a language with dynamic typing like JavaScript, it's often necessary (and a very good idea) to test the type of variable before performing any operations on it. Although it might not seem like much to test for the type of a variable, it isn't always a freebie, and in practice can simply result in annoyances and bugs because of subtle differences. Base provides a few handy functions to simplify the nuances entailed. Like the other issues we've touched on so far, there are subtleties amongst various browsers involving some of the finer points. The following list summarizes:
isString(/*Any*/ value)
Returns
true
ifvalue
is aString
.isArray(/*Any*/ value)
Returns
true
ifvalue
is anArray
.isFunction(/*Any*/ value)
Returns
true
if value is aFunction
.isObject(/*Any*/ value)
Returns
true
ifvalue
is anObject
(including anArray
andFunction
) ornull
.isArrayLike(/*Any*/ value)
Returns
true
if value is anArray
but also allows for more permissive possibilities. For example, the built-inarguments
value that can be accessed from within a Function object is especially an oddball in that it does not support built-in methods such aspush
; however, it is array-like in that it is a list of values that can be indexed.isAlien(/*Any*/ value)
Returns
true
if value is a built-in function or native function such as an ActiveX component but does not respect the normal checks such as theinstanceof Function
.
A concept commonly involved in dynamic programming languages like Python and JavaScript called duck typing provides a common thread for many of the functions just introduced. Duck typing is based upon the saying that if it walks like a duck and quacks like a duck, then it's a duck. Basically, what that means is that if a particular data member exhibits the minimal necessary properties to be a particular data type, then that's good enough to assume that it is that particular data type.
For example, the built-in arguments
member qualifying as an array
via the isArrayLike
function
hopefully ties this theme together. When you consider the inherent
dynamism in a language that does not require you to declare a
particular variable to always be a particular data type (dynamic
binding), duck typing is a great vehicle to inspect the type of an
object when necessary.
For example, invoking the typeof
operator on an ordinary array such
as []
returns object
while Base's isArray
function performs some duck type
checks behind the scenes and returns true
for an array such as []
.
Tip
Duck typing is a fundamental programming concept in JavaScript and much of the toolkit, so this discussion is more practical to your day-to-day programming than you might imagine at first glance.
The bottom line is that Base's type checking functions can save you time and spare you from nonintuitive browser inconsistencies, so use them well, and use them often.
Get Dojo: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.