Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Excerpt

An alternative to Returns a more accurate object type name than Javascript's native "typeof" that provides more consistent resultsoperator...

 

Syntax

typeOf() can be used as a global function, or as a method of any object instance...

Code Block
themeRDark
languagejavascript
linenumberstrue
var returnValue = typeOf(obj);
// or...
var returnValue = obj.typeOf();

Parameters

Parameter / ObjectTypeMandatoryDescriptionUtil.js versionAPI Version
objVariant(error)

By default, typeOf() will return the type of 'this' – the object that it is a method of.

If you pass in an object of any kind as a parameter, typeOf() will return the name of that object's constructor.

1.0

Return

valye

value

Returns the string name of the constructor of the class from which the object was derived (or primitive value type coerced to)instantiated.

(warning) If the object is a number, and it's value is NaN, typeOf() will return "nan" instead of "number".

Examples

Car Care must be taken when checking the type of primitive values...

Code Block
themeRDark
languagejavascript
linenumberstrue
// don't do this:
var returnValue = 5.typeOf(); // Syntax Error

// instead, do this:
var returnValue = typeOf(5); // "number"
var returnValue = (5).typeOf(); // "number"
 
// Or if you want a l33t way of doing it:
var returnValue = 5..typeOf(); // "number" (:

The following examples show the key differences between Javascript's typeof operator and the typeOf() function...If you create a class and instantiate objects from it, typeOf() will return "object" not the name of the class.

Code Block
themeRDark
languagejavascript
linenumberstrue
function FooClass() {};
FooClass.typeof(); // "function"
typeof FooClass; // "function"
 
var foo = new FooClass;
foo.typeOf(); // "object" because foo is an object
typeof foo; // "object" because foo is an object
Div
classbox
Availability

This feature requires:

  • Util.js v0.3 and above.
  • Can be accessed via global object/scope.
    Status
    colourGreen
    titleStable

    Requires:

    Div
    classbox
    Contents

    Jump to:

    Table of Contents
    maxLevel5

    Div
    classbox

    Type Checking

    Topics:

    Child pages (Children Display)
    alltrue
    depthall
    pageType Checking
    excerpttrue

    Div
    classbox

    See also

    Related articles:

    • Fixing the Javascript typeof operator – the blog post which my uGetTypethat inspired typeOf() function is based on (plus loads of additional information)Object.uInherit
    • function.inherit() – works perfectly with typeOf() because it is based on classical prototypal inheritance model

     The following examples show the key differences between Javascript's typeof operator and the typeOf() function...

    Section
    Column
    width47%
    Code Block
    themeRDark
    languagejavascript
    titleJavascript's typeof operator
    linenumberstrue
    typeof {a: 4}; //"object"
    typeof [1, 2, 3]; //"object"
    typeof function(){}; // "function"
    (function() {
      return typeof arguments})(); //"object"
    })();
    typeof new ReferenceError; //"object"
    typeof new Date; //"object"
    typeof /a-z/; //"object"
    typeof Math; //"object"
    typeof JSON; //"object"
    typeof NaN; //"number"
    typeof 4; //"number"
    typeof new Number(4); //"object"
    typeof "abc"; //"string"
    typeof new String("abc"); //"object"
    typeof true; //"boolean"
    typeof new Boolean(true); //"object"
    typeof global //"object"
    typeof null; //"null"
    typeof undefined; //"undefined"
    Column
    width53%
    Code Block
    themeRDark
    languagejavascript
    titletypeOf(stuff)
    linenumberstrue
    typeOf({a: 4}); //"object"
    typeOf([1, 2, 3]); //"array"
    typeOf(function(){}); // "function"
    (function() {
      return typeOf(arguments)})(); //"arguments"
    })();
    typeOf(new ReferenceError); //"error"
    typeOf(new Date); //"date"
    typeOf(/a-z/); //"regexp"
    typeOf(Math); //"math"
    typeOf(JSON); //"json"
    typeOf(NaN)' //"nan"
    typeOf(4); //"number"
    typeOf(new Number(4)); //"number"
    typeOf("abc"); //"string"
    typeOf(new String("abc")); //"string"
    typeOf(true); //"boolean"
    typeOf(new Boolean(true)); //"boolean"
    typeOf(global); //"global"
    typeOf(null); //"null"
    typeOf(undefined); //"undefined"