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

Type Checking

Additional methods of type checking...

 

Overview

Javascript's typeof operator is widely used for type checking, but its not particularly semantic and in some cases it's results are very misleading (because it returns the type of the instance, not the class it was derived from).

Util API provides some additional methods for checking data types:

  • object.isArray – quickly find out if an object is an array
  • object.isFunction – quickly find out if an object is a function
  • typeOf() – returns the name of the constructor used to create an object

Example

function foo(something) {
	arguments.isArray; // false
	arguments = toArray(arguments);
	arguments.isArray; // true
 
	switch (typeOf(something)) {
		case "function": return "it's a function";
		case "global": return "it's the global object";
		case "regexp": return "it's a regular expression";
		// ...etc...
		default: return "it's a "+typeOf(something);
	}
}
 
foo.isFunciton; // true
Availability STABLE

Requires:

Contents

Jump to:

Type Checking

Topics:

  • classOf()Returns the name of the class (constructor) used to create an object...
  • object.isFunctionA 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.isArrayA quick way to check if an object is an array...