(info) This AI is still in early stages of development.

classOf()

Returns the name of the class (constructor) used to create an object...

 

Syntax

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

var returnValue = classOf(obj);
// or...
var returnValue = obj.classOf();

Parameters

ParameterTypeMandatoryDescriptionAPI Version
objVariant(error)

The object or value to get the class (constructor) name of.

1.1

Return value

ValueTypeNotesAPI Version
<returnValue>String

The name of the class (constructor) used to instantiate the object or value.

  • Unlike typeOf(), the name is not converted to lower case. Eg. A number will have classOf() "Number" not "number", a regular expression will be "RegExp" and not "regexp", etc.
  • classOf(NaN) == "Number" (because the number class created NaN)
  • The values null and undefined return pseudo-class names of "Null" and "Undefined" respectively
  • Anonymous classes (constructor present but not named) will return "<Anonymous>"
1.1

Examples

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

// 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" (:

If you create a class and instantiate objects from it, classOf() will return the name of the constructor unless it is anonymous:

var Foo = function FooClass() {}; // named constructor "FooClass"
var Bar = function() {}; // anonymous constructor
 
var foo = new FooClass;
foo.classOf(); // "FooClass"
 
var bar = new Bar;
bar.classOf(); // "<Anonymous>"
Availability STABLE

Requires:

Contents

Jump to:

Type Checking

Topics:

  • classOf() — Returns the name of the class (constructor) used to create an object...
  • object.isFunction — A quick way to check if an object is a function...
  • typeOf() — Returns a more accurate object type name than Javascript's native "typeof https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof" operator...
  • object.isArray — A quick way to check if an object is an array...

See also

Related articles:

  • typeOf() – an alternative to Javascript's typeof operator
  • function.inherit() – works perfectly with classOf() because it is based on classical prototypal inheritance model

The following examples compare typeOf() and classOf()...

typeOf(stuff)
typeOf({a: 4}); //"object"
typeOf([1, 2, 3]); //"array"
typeOf(function(){}); // "function"
(function() {
  return typeOf(arguments); //"arguments"
})();
typeOf(new TypeError); //"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"
classOf(stuff)
classOf({a: 4}); //"Object"
classOf([1, 2, 3]); //"Array"
classOf(function(){}); // "Function"
(function() {
  return typeOf(arguments); //"Object"
})();
classOf(new TypeError); //"TypeError"
classOf(new Date); //"Date"
classOf(/a-z/); //"RegExp"
classOf(Math); //"Object"
classOf(JSON); //"Object"
classOf(NaN)' //"Number"
classOf(4); //"Number"
classOf(new Number(4)); //"Number"
classOf("abc"); //"String"
classOf(new String("abc")); //"String"
classOf(true); //"Boolean"
classOf(new Boolean(true)); //"Boolean"
classOf(global); //"Object"
classOf(null); //"Null"
classOf(undefined); //"Undefined"