0
function isDataType(dataType, obj) {
 return Object.prototype.toString.call(obj) == '[object' + dataType + ']';
}
var arr = [];
alert(isDataType("Array", arr)); // alerts 'false' which is false

When I make obj equal to an array and make the data type to evaluate as an array, it still says false. Is there a way to fix this? Thanks.

asked Apr 18, 2011 at 23:07
1
  • When you alert just Object.prototype.toString.call(obj) what do you get? Is the value what you expect? Commented Apr 18, 2011 at 23:12

5 Answers 5

2

Yes - don't use that method to find datatypes when finding datatypes of Arrays. Instead use arr instanceof Array.

answered Apr 18, 2011 at 23:09
Sign up to request clarification or add additional context in comments.

6 Comments

Can you give me the complete edited code so I understand completely? That would be most helpful. Thanks.
var arr = []; alert(arr instanceof Array); // true, which is true
instanceof will give you problems checking for arrays that come from a different environment (e.g. a different browser frame), IE has its own problems even with Object.prototype.toString. Give a look to this post. Anyway, if the user is working in a non cross-frame environment instanceof will work.
So is this good: function isDataType(dataType, obj) { return obj instanceof dataType; }
Not if you continue passing strings. That also won't work for Strings and all other native types, so you might as well simply remove the function altogether and use the appropriate type-matching method (typeof ... === '...' or ... instanceof ...) instead.
|
1

You are missing a space after '[object. Your code should then evaluate to true.

You should use instanceof to find out if an object is of a specific type, though.

answered Apr 18, 2011 at 23:11

2 Comments

Can you give me the complete edited code so I understand completely what you mean to use 'instanceOf'?
You can replace your whole isDataType method by simply using arr instanceof Arrayor such. See this for more: javascript.about.com/od/reference/g/rinstanceof.htm
1

Don't you just need another space?

'[object ' + dataType + ']'
 ^-- a space here

This isn't a great method for testing for datatypes, as others have already mentioned.

answered Apr 18, 2011 at 23:11

1 Comment

Thanks, that fixed it. I hate making stupid minor errors like that!!
0
function is(type, obj) {
 var clas = Object.prototype.toString.call(obj).slice(8, -1);
 return obj !== undefined && obj !== null && clas === type;
}
is('String', 'test'); // true
is('String', new String('test')); // true
answered Apr 18, 2011 at 23:18

Comments

0
function whatsit(what){
 if(what== null) return String(what);
 what= what.constructor;
 var Rx= /function\s+([^(\s]+)\s*\(/, 
 tem= String(what).match(Rx);
 if(tem) return tem[1];
 return String(what);
}
answered Apr 19, 2011 at 1:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.