Determining Object Type with MooTools’ typeOf

By  on  

One thing about JavaScript I dislike is the vagueness of what the typeof operator returns. Pass typeof an array? You get "object" back (which it is, but a more concise answer would be helpful). Pass typeof a Date object? You get "object" again. What if there was a better way of determining an object's descriptive type? That's where the typeOf function within MooTools Core comes into play.

typeOf Source and Usage

The typeOf function is actually quite short:

var typeOf = this.typeOf = function(item){
	if (item == null) return 'null';
	if (item.$family) return item.$family();

	if (item.nodeName){
		if (item.nodeType == 1) return 'element';
		if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
	} else if (typeof item.length == 'number'){
		if (item.callee) return 'arguments';
		if ('item' in item) return 'collection';
	}

	return typeof item;
};

typeOf checks for specific properties on the object in question to determine its descriptive type. Simple, right? Note the $family() check within typeOf; each Type (Array, Function, Date, etc.) instance is given a $family method which returns its type. Let's try a few typeOf calls:

typeof document.body;  // returns "object"
typeOf(document.body);  // returns "element"

typeof new Date();  // returns "object"
typeOf(new Date());  // returns "date"

typeof [];  // returns "object"
typeOf([]);  // returns "array"

typeOf is an awesome utility function, right? Getting a more detailed object type than simply "object" can be hugely help in validating the object before using it. typeOf is just another awesome utility within the MooTools JavaScript framework.

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Create Classy Inputs Using MooTools’ OverText

    The MooTools More library is a goldmine. A treasure chest. Pirates booty, if you will (and, of course, I will). More is full of plugins that add a lot of class and functionality to your website with minimal effort.

  • By
    Generate Dojo GFX Drawings from SVG Files

    One of the most awesome parts of the Dojo / Dijit / DojoX family is the amazing GFX library.  GFX lives within the dojox.gfx namespace and provides the foundation of Dojo's charting, drawing, and sketch libraries.  GFX allows you to create vector graphics (SVG, VML...

Discussion

  1. I use the regular “typeOf” to determine if classes are present, but the MooTools typeOf is much better, and great for making sure variables are the correct type. I wish more plugin coders would return robust error messages with these kinds of checks.

  2. Nice one !!!!!!

  3. Lorenzo

    Anyone knows if Mootools’ typeOf is more reliable than standard typeof when I want to check if something is a function or not?

    var a = (function() { return true; });
    var b = { name: 'value' };
    // typeof(a) == typeOf(a) == 'function' is always TRUE?
    // typeof(b) == typeOf(b) != 'function' is always TRUE?
    
  4. Mootools supports another type check – the Type Object.
    Type.isNumber(var), Type.isObject() etc.

    This is nowhere in the docs (except once used in an example on the Array page). It is shorter and more semantic when you are checking for one type, don’t know why it is hidden.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!