typeOf - simple yet robust


/ Published in: JavaScript
Save to your folder(s)

Brilliant solution using Object.prototype.toString()

Test cases included (commented out)


Copy this code and paste it in your HTML
  1. /* Robust typeof v3.0
  2.  *
  3.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License
  4.  * http://creativecommons.org/licenses/by/3.0/
  5.  *
  6.  * Author: Andy Harrison, http://dragonzreef.com/
  7.  * Date: 16 September 2011
  8.  */
  9.  
  10. //based on the brilliant solution of using Object.prototype.toString() from
  11. // http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
  12.  
  13. function typeOf(o)
  14. {
  15. var undefined;
  16. if(o === undefined) return "undefined";
  17. if(o === null) return "null";
  18. return Object.prototype.toString.call(o).replace(/\[object (.*?)\]/, "$1");
  19. }
  20.  
  21. function isUndefined(o){ return typeOf(o) == "undefined"; }
  22. function isNull(o){ return typeOf(o) == "null"; }
  23.  
  24. function isBoolean(o){ return typeOf(o) == "Boolean"; }
  25. function isNumber(o){ return typeOf(o) == "Number"; }
  26. function isString(o){ return typeOf(o) == "String"; }
  27. function isRegExp(o){ return typeOf(o) == "RegExp"; }
  28. function isDate(o){ return typeOf(o) == "Date"; }
  29. function isArray(o){ return typeOf(o) == "Array"; }
  30. function isFunction(o){ return typeOf(o) == "Function"; }
  31. function isObject(o){ return typeOf(o) == "Object"; }
  32.  
  33. Object.prototype.isBoolean = function(){ return typeOf(this) == "Boolean"; };
  34. Object.prototype.isNumber = function(){ return typeOf(this) == "Number"; };
  35. Object.prototype.isString = function(){ return typeOf(this) == "String"; };
  36. Object.prototype.isRegExp = function(){ return typeOf(this) == "RegExp"; };
  37. Object.prototype.isDate = function(){ return typeOf(this) == "Date"; };
  38. Object.prototype.isArray = function(){ return typeOf(this) == "Array"; };
  39. Object.prototype.isFunction = function(){ return typeOf(this) == "Function"; };
  40. Object.prototype.isObject = function(){ return typeOf(this) == "Object"; };
  41.  
  42. if(!Array.isArray) Array.isArray = isArray;
  43.  
  44. /*
  45. //test all data types
  46. //IE 8 and older give "Object" as the type of an HTMLDivElement
  47. var u;
  48. function f(){}
  49. var testValues = [
  50. {value: u, code: "= undefined", expected: "undefined"},
  51. {value: null, code: "= null", expected: "null"},
  52. {value: true, code: "= true", expected: "Boolean"},
  53. {value: (new Boolean), code: "= new Boolean", expected: "Boolean"},
  54. {value: 1, code: "= 1", expected: "Number"},
  55. {value: (new Number), code: "= new Number", expected: "Number"},
  56. {value: "s", code: "= \"s\"", expected: "String"},
  57. {value: (new String), code: "= new String", expected: "String"},
  58. {value: /r/, code: "= /r/", expected: "RegExp"},
  59. {value: (new RegExp), code: "= new RegEx)", expected: "RegExp"},
  60. {value: (new Date), code: "= new Date", expected: "Date"},
  61. {value: [], code: "= []", expected: "Array"},
  62. {value: [0,1], code: "= [0,1]", expected: "Array"},
  63. {value: (new Array), code: "= new Array", expected: "Array"},
  64. {value: f, code: "function f(){}", expected: "Function"},
  65. {value: function(){}, code: "= function(){}", expected: "Function"},
  66. {value: (new Function), code: "= new Function", expected: "Function"},
  67. {value: {}, code: "= {}", expected: "Object"},
  68. {value: (new Object), code: "= new Object", expected: "Object"},
  69. {value: (new f), code: "= new f", expected: "Object"},
  70. {value: document.createElement("div"), code: "= document.createElement(\"div\")", expected: "HTMLDivElement"}
  71. ];
  72.  
  73. var t = "<table><tr><th>Value</th><th>Code</th><th>Expected</th><th>Result</th></tr>";
  74. var c;
  75. for(var i=0; i<testValues.length; i++)
  76. {
  77. c = " style=\"color:green\"";
  78. if(testValues[i].expected != typeOf(testValues[i].value)) c = " style=\"color:red\"";
  79. t += "<tr><td>"+testValues[i].value+"</td><td>"+testValues[i].code+"</td><td>"+testValues[i].expected+"</td><td"+c+">"+
  80. typeOf(testValues[i].value)+"</td></tr>";
  81. }
  82. t += "</table>";
  83. document.getElementById("log").innerHTML += t;
  84. */

URL: http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.