Matt Snider JavaScript Resource

Understanding JavaScript and Frameworks

Wednesday, April 4, 2007

Type Detection

Common JavaScript Interview question: What are the object types in JavaScript?

The short answer is that there is only one type in JavaScript: Object. However, this is not really the whole truth, as JavaScript has 7 meta-types: Number, String, Boolean, Function, Object, Null, Undefined.

Since JavaScript is not a strongly typed language and in fact every object descends from the ‘Object’ type, you may find it helpful to manage types yourself. I personally, always manage types as it allows me to bulletproof my JavaScript functions and prevent other developers from breaking my code. Also, AJAX applications often return bad data because lot can go wrong with the request and server-side code execution. I have developed a simple set of functions that I always include in my ‘core.js’

Below is a list of functions that I use to detect object types:

isAlien:
/* True, if the object is not part of a function. */

function isAlien(o) {return isObject(o) && !isFunction(o.constructor);}

isArray:
/* True, if the object is constructed of the native Array Object */

function isArray(o) {return isObject(o) && o.constructor == Array;}

isBoolean:
/* True, if the object is of the meta-type ‘boolean’ */

function isBoolean(o) {return ‘boolean’ == typeof o;}

isDate:
/* True, if the object is an Object with the getMonth method */

function isDate(o) {return isObject(o) && o.getMonth;}

isDomElement:
/* True, if the object is set and has children nodes or a node type */

function isDomElement(o) {return o && (”undefined” !== typeof o.childNodes || o.nodeType);}

isEvent:
/* True, if the object is set, the native Event Object is defined, and the object has an event phase */

function isEvent(o){return o && “undefined” != typeof Event && o.eventPhase;}

isNull:
/* True, if the object is null */

function isNull(o) {return null === o;}

isFunction:
/* True, if the object is of the meta-type 'function' */

function isFunction(o) {return ‘function’ == typeof o;}

isNumber:
/* True, if the object is of the meta-type ‘number’ and is finite as determined by the native function */

function isNumber(o) {return “number” == typeof o && isFinite(o);}

isObject:
/* True, if the object is of the meta-type ‘object’ or ‘function’ */

function isObject(o) {return (o && “object” == typeof o) || isFunction(o);}

isSet:
/* True, if the object has 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));}

isString:
/* True, if the object is of the meta-type 'string' */

function isString(o) {return ’string’ == typeof o;}

isUndefined:

/* True, if the object is of the meta-type 'undefined' */

function isUndefined(o) {return ‘undefined’ == typeof o;}

isEmpty:

/* True, if the object does not have any members that are not functions */

function isEmpty(o) {
var i, v;
if (isObject(o)) {
for (i in o) {
v = o[i];
var t = isUndefined(v);
var t1 = isFunction(v);
if (! isUndefined(v) && ! isFunction(v)) {
return false;
}
}
}
return true;
}

*note: 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, like Date and Array, when writing application objects.

posted by Matt Snider at 4:05 pm  

6 Comments »

  1. Here’s a more robust isDate implementation:

    return (o != null && typeof o == ‘object’ && o.constructor.toString() == Date.toString());

    The same principle can be used for all isTYPE implementations.

    Cheers,
    m

    Comment by Mikol Graves — September 9, 2007 @ 7:17 am

  2. Thanks Mikol. This looks simple and impressive. I’m going to use this as material for my Tuesday article this week.

    Comment by admin — September 9, 2007 @ 11:30 am

  3. […] Graves, recently commented on my Type Detection article showing a novel way to do type detection. Here is his code […]

    Pingback by Type Detection Revisited | Matt Snider JavaScript Resource — September 11, 2007 @ 10:09 pm

  4. There are more built-ins than you listed.

    Global
    Object
    Function
    Array
    String
    Boolean
    Number
    Math
    Date
    RegExp
    All the Error objects (EvalError, RangeError, et c.)

    Your typechecking functions won’t work across frames.

    Comment by Garrett — October 10, 2007 @ 4:14 pm

  5. Garrett, while it is true that there are many other “types” in JavaScript (and you can create an unlimited number yourself). These 7 meta-types are native Objects and/or have special properties that make them unique. I have also revisited this subject here:

    http://mattsnider.com/core/type-detection-revisited/

    Comment by admin — October 15, 2007 @ 3:57 pm

  6. It helps me a lot.. Tnx.. :)

    Comment by Andrew III — November 11, 2007 @ 6:52 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress