cluster1
cluster1

Reputation: 5694

JavaScript: Can one use "Object.prototype.toString.call(obj)" for data-type detection?

If one calls the toString-method of Object together with call (for changing the context) then a string is returned. This string contains the data-type of the value which was given to call.

var obj = {};
var field = [];
var str = 'abc';
var num = 3;
var reg = /.*/;
var bool = true;
var notANumber = NaN;
var undef = undefined;

console.log(Object.prototype.toString.call(obj)); // [object Object]
console.log(Object.prototype.toString.call(field)); // [object Array]
console.log(Object.prototype.toString.call(str)); // [object String]
console.log(Object.prototype.toString.call(num)); // [object Number]
console.log(Object.prototype.toString.call(reg)); // [object RegExp]
console.log(Object.prototype.toString.call(bool)); // [object Boolean]
console.log(Object.prototype.toString.call(notANumber)); // [object Number]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]

What I can say from my tinkering: Beside the usual JavaScript weirdness (NaN is Number) it seems to work fine.

Now I ask myself:

If I make a function with some .split(/\s/) etc., returning the data-type as a string then I could use that for type-detection.

But what makes me distrustful:

I have never seen that programmers use the Object.prototype.toString.call-construct that way. They use typeof or look for certain methods ...

So my question is:

Would a function like described above work as expected?

Or would it fail because of different JavaScript implementations in the browser? Or fail because of some other reason?

Upvotes: 3

Views: 138

Answers (1)

Emil S. Jørgensen
Emil S. Jørgensen

Reputation: 6366

According to the specifications at MDN, Object.prototype.toString is fully supported on all browsers, so this should work consistently.

I would like to point out that only detecting the difference between an array and an object would differentiate the results from simply using typeof:

EDIT 1

And the reg variable also detects as an object.

var obj = {};
var field = [];
var str = 'abc';
var num = 3;
var reg = /.*/;
var bool = true;
var notANumber = NaN;
var undef = undefined;

console.log(typeof obj); // object
console.log(typeof field); // object
console.log(typeof str); // string
console.log(typeof num); // number
console.log(typeof reg); // object
console.log(typeof bool); // boolean
console.log(typeof notANumber); // number
console.log(typeof undefined); // undefined
//Data from original post:
console.log(Object.prototype.toString.call(obj)); // [object Object]
console.log(Object.prototype.toString.call(field)); // [object Array]
console.log(Object.prototype.toString.call(str)); // [object String]
console.log(Object.prototype.toString.call(num)); // [object Number]
console.log(Object.prototype.toString.call(reg)); // [object RegExp]
console.log(Object.prototype.toString.call(bool)); // [object Boolean]
console.log(Object.prototype.toString.call(notANumber)); // [object Number]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]

Upvotes: 2

Related Questions