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
David G
97.6k41 gold badges173 silver badges258 bronze badges
-
When you alert just Object.prototype.toString.call(obj) what do you get? Is the value what you expect?Jason Yost– Jason Yost2011年04月18日 23:12:10 +00:00Commented Apr 18, 2011 at 23:12
5 Answers 5
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
David G
Can you give me the complete edited code so I understand completely? That would be most helpful. Thanks.
Ry-
var arr = []; alert(arr instanceof Array); // true, which is trueChristian C. Salvadó
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.David G
So is this good: function isDataType(dataType, obj) { return obj instanceof dataType; }
Ry-
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. |
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
weltraumpirat
22.6k5 gold badges44 silver badges54 bronze badges
2 Comments
David G
Can you give me the complete edited code so I understand completely what you mean to use 'instanceOf'?
weltraumpirat
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 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
Dominic Barnes
28.5k8 gold badges69 silver badges91 bronze badges
1 Comment
David G
Thanks, that fixed it. I hate making stupid minor errors like that!!
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
danniel
1,7511 gold badge11 silver badges13 bronze badges
Comments
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
kennebec
105k32 gold badges109 silver badges127 bronze badges
Comments
lang-js