Created
April 6, 2016 19:18
-
-
Save andreasonny83/64b9d66233fd50367145f0b762a316ac to your computer and use it in GitHub Desktop.
Using toString() to detect object class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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