fork download
  1. function getType(data) {
  2. //получили строку вида "[Object Type]"
  3. var type = Object.prototype.toString.call(data);
  4. //оставили только "Type"
  5. type = type.slice(8, -1);
  6. //перевели в нижний регистр первый символ
  7. type = type[0].toLowerCase() + type.slice(1);
  8. /*
  9.   * здесь используем typeOf, потому что
  10.   * toString.call() возвращает
  11.   * для arguments значение не "object",
  12.   а "arguments"
  13.   */
  14. if (typeof data == "object" && data.length > 0) {
  15. type = "array-like";
  16. }
  17. return type;
  18. }
  19.  
  20. console.log(getType(13));
  21. console.log(getType("I'm a string"));
  22. console.log(getType([1, 2, 3]));
  23. console.log(getType(true));
  24. console.log(getType(undefined));
  25. console.log(getType(function() {var x = 2 + 2; }));
  26. console.log(getType({foo: 1, bar: 2}));
  27.  
  28. function getArguments(x, y) {
  29. return arguments;
  30. }
  31. var args = getArguments(1, 3);
  32.  
  33. console.log(getType(args));
  34.  
Success #stdin #stdout 0.06s 20312KB
stdin
Standard input is empty
stdout
number
string
array-like
boolean
undefined
function
object
array-like