Skip to content

Instantly share code, notes, and snippets.

@andreasonny83
Created April 6, 2016 19:18
Show Gist options
  • Save andreasonny83/64b9d66233fd50367145f0b762a316ac to your computer and use it in GitHub Desktop.
Save andreasonny83/64b9d66233fd50367145f0b762a316ac to your computer and use it in GitHub Desktop.
Using toString() to detect object class
// Using toString() to detect object class
// toString() can be used with every object and allows you to get its class. To use the Object.prototype.toString() with every object, you need to call Function.prototype.call() or Function.prototype.apply() on it, passing the object you want to inspect as the first parameter called thisArg.
var toString = Object.prototype.toString;
toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]
// Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment