0

I am trying to get the type of an argument by not using the typeof. Also (its part of an exercise) I must use Object.prototype.toString...

Calling the function should return (for example if its a string) "String". However, my code returns String]

How can I remove the bracket? Thanks!

function types(x){

   var array = Object.prototype.toString.call(x);
    var arr= array.split(" ");
   return arr[1];
}

types("hello");

learningcoding
  • 187
  • 2
  • 14
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr – Marty May 22 '16 at 23:10
  • 5
    Just `Object.prototype.toString.call(x).slice(8, -1)` – Bergi May 22 '16 at 23:22
  • That doesn't work! I want it of course to be general- for a string it should return "String" for Number 'Number' etc. If I use substring .substring(start,end) -for example "Number" and "String" have a different length so thats pointless – learningcoding May 22 '16 at 23:23
  • 1
    This problem is solved in other posts. look here http://stackoverflow.com/a/7390612/5728894 – Matteo Brighi May 22 '16 at 23:30
  • .slice(0,-1); Will always return the whole string except the last character. – sebenalern May 22 '16 at 23:31
  • yea it actually works if I remove the split()-and only because I know how many letters the word object includes so I can say slice(8,-1) otherwise it wouldn't work – learningcoding May 22 '16 at 23:37
  • Take a look at my edit. – sebenalern May 22 '16 at 23:38
  • Seems an odd task. What if the object in question implements `toString()` and returns something else? – Phil May 23 '16 at 00:09
  • 2
    @learningcoding: The point is that the `[object ` prefix always is the same. It *will* work,being both simple and efficient. You don't need to `split` anything dynamically. – Bergi May 23 '16 at 00:17
  • 2
    @Phil: That's exactly why you call `Object.prototype.toString` upon the object, instead of invoking its own `toString` method. – Bergi May 23 '16 at 00:20
  • @Bergi ah, I missed that. Good point – Phil May 23 '16 at 01:05
  • you should first `delete Object.prototype.toString;` before you use `Object.prototype.toString.call()` in case another script mucked it up. also, why not just use `.replace(/\W/g,"")` on any output if unsure of the format? – dandavis May 23 '16 at 01:23

2 Answers2

0

I don't know why you have to use Object.prototype.toString as native string is not an object. If you force it on a string, then it will return the "[object String]".

If you want the type of a variable, then you can simply use the following function

var types = x => x.contructor.name;

If you must, then you use the following

var types = x => Object.prototype.toString.call(x).match(/\[object (.*)\]/)[1];

This is inefficient as you first wrap the original variable with an object variable, then call its toString function, followed by a regExp (or substr).

Hin Fan Chan
  • 1,543
  • 10
  • 11
0

This will return String. Uses slice() to remove the trailing ]

function types(x) {
   var array = Object.prototype.toString.call(x);
   var arr = array.split(" ");
   return arr[1].slice(0, -1);
}
console.log(types('hello')) // String
mferly
  • 1,646
  • 1
  • 13
  • 19