A common JavaScript Interview question is:
“What are the object types in JavaScript?”
The real answer is that everything in JavaScript is an Object. However, that is not the whole truth, as JavaScript has 7 meta-types: number
, string
, boolean
, function
, object
, null
, undefined
.
Since JavaScript is not a strongly typed language, you may find it useful to manage object
types yourself. For my projects, I always manage object
types as it allows me to bulletproof my JavaScript functions and prevent other developers from misusing the code I share. Also, AJAX applications often return bad data because a lot can go wrong with the request and server-side code execution.
Below is a list of functions I previously used to detect object
types:
Example 1: isAlien
/* True whenois not a member of a function. */ function isAlien(o) {return isObject(o) && ! isFunction(o.constructor);}
Example 2: isArray
/* True whenois a native JavaScript array */ function isArray(o) {return isObject(o) && o.constructor == Array;}
Example 3: isBoolean
/* True whenois the meta-typeboolean*/ function isBoolean(o) {returnboolean=== typeof o;}
Example 4: isDate
/* True whenois an object having thegetMonthmethod */ function isDate(o) {return isObject(o) && o.getMonth;}
Example 5: isDomElement
/* True whenois set and has children nodes or a node type */ function isDomElement(o) {return o && ("undefined" !== typeof o.childNodes || o.nodeType);}
Example 6: isEvent
/* True whenois set, the native JavaScript event is defined, andohas an event phase */ function isEvent(o){return o && "undefined" != typeof Event && o.eventPhase;}
Example 7: isNull
/* True whenois exactly equal tonull*/ function isNull(o) {return null === o;}
Example 8: isFunction
/* True whenois the meta-typefunction*/ function isFunction(o) {returnfunction== typeof o;}
Example 9: isNumber
/* True whenois the meta-typenumberand is finite */ function isNumber(o) {return "number" == typeof o && isFinite(o);}
Example 10: isObject
/* True whenois the meta-typeobjectorfunction*/ function isObject(o) {return (o && "object" == typeof o) || isFunction(o);}
Example 11: isSet
/* True whenohas any value, including 0 and false, both of which are normally falsy */ function isSet(o) {return ((o || false === o || 0 === o) && ! isNull(o) && !== o && ! isUndefined(o));}
Example 12: isString
/* True whenois of the meta-typestring*/ function isString(o) {returnstring== typeof o;}
Example 13: isUndefined
/* True whenois of the meta-typeundefined*/ function isUndefined(o) {returnundefined== typeof o;}
Example 14: isEmpty
/* True whenodoes not have any member values set that are not functions */ function isEmpty(o) { var i, v; if (isObject(o)) { for (i in o) { v = o[i]; if (! isUndefined(v) && ! isFunction(v)) { return false; } } } return true; }
Notes:
I no longer use these methods as I prefer to use the YUI library as the backbone of my JavaScript code. Similar methods are available on YUI Lang. And you can always extend theYAHOO.lang
object with extra type detection methods.most of these functions are not bulletproof and can be fooled. For example:
var fakeDate = { getMonth: 1234 }; isDate(fakeDate); /* this will be true */
I have not found a good way to prevent this type of abuse, except through intelligent coding. You should never use reserved names or those names used by other native JavaScript objects, such as
date
and array
, when writing application objects.