Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 0.57 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var o = {};
  2.  
  3. function getIsType(type) {
  4.     return function (o) {
  5.         return /* undefined */ typeof o === type ||
  6.             /* null */ o === null && type === "null" ||
  7.             Object.prototype.toString.call(o).match(/\s+(\w+)/)[1].toLowerCase() === type;
  8.     };
  9. }
  10.  
  11. var types = "String Number Date Object Array RegExp Function Null Undefined Boolean".split(" ");
  12.  
  13. for (var i in types) {
  14.     if (types.hasOwnProperty(i)) {
  15.         o["is" + types[i]] = getIsType(types[i].toLowerCase());
  16.     }
  17. }
  18.  
  19. // o.isString("")  === true
  20. // o.isNull(null)  === true
  21. // o.isUndefined() === true