admin管理员组文章数量:1424929
I have try three way in the bellow, the result is ment on the right,the diffcult is I can't different it from Object
datatype.How can I get it datatype like Array
or String
but not Object
?
var arr = [];
console.log('type of arr', typeof arr); //objct
console.log('instanceof array', arr instanceof Array); //true
console.log('instanceof object', arr instanceof Object); // true
I have try three way in the bellow, the result is ment on the right,the diffcult is I can't different it from Object
datatype.How can I get it datatype like Array
or String
but not Object
?
var arr = [];
console.log('type of arr', typeof arr); //objct
console.log('instanceof array', arr instanceof Array); //true
console.log('instanceof object', arr instanceof Object); // true
Share
Improve this question
asked Jun 13, 2012 at 5:46
hh54188hh54188
15.7k35 gold badges116 silver badges192 bronze badges
1
- By the way, this has already been asked. – voithos Commented Jun 13, 2012 at 5:57
5 Answers
Reset to default 3Here is one technique:
> var arr = [];
> Object.prototype.toString.call(arr);
"[object Array]"
What this does is call the toString
method of the prototype object, using whatever is passed in as the this
pointer. For more information on this technique, see the reference on call
.
It turns out that you can use this technique to figure out other object types as well:
> var func = function(){}
> Object.prototype.toString.call(func);
"[object Function]"
> var obj = {};
> Object.prototype.toString.call(obj);
"[object Object]"
you can use jQuery's "isArray" function for this
var arr1 =[];
alert(jQuery.isArray(arr1)); // true
var arr2 = new Array();
alert(jQuery.isArray(arr2)); // true
var obj = new Object();
alert(jQuery.isArray(obj)); // false
I got this info at MDN - In Javascript Version 1.8.5 Array.isArray is added and it is standard in ECMAScript 5
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray( new Array() );
Array.isArray( Array.prototype ); // Little known fact: Array.prototype itself is an array.
Also if isArray is not available
if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
For more details at MDN
Array always will be instance of object - Object
is the base object in javascript and any another object created by new is inherited from.
new String('a') instanceof Object // true - also instance of String
new Number(3) instanceof Object // true -also instance of Number etc.
new Boolean(true) instanceof Object // true
new Date instanceof Object // true
new function(){} instanceof Object // true
[] instanceof Object // true - [] is equal to new Array
check this out:
Array = 1;
[] //TypeError: Cannot read property 'slice' of undefined
:)
however
'a' instanceof Object // false
3 instanceof Object // false
Try this:
var str = 'aaa',
arr = [],
myClass = function(){},
myClassInstance = new myClass;
str.constructor == String // true
arr.constructor == Array // true
myClassInstance.constructor == myClass // true
This is very simple, your question is your answer indeed,
var arr = [];
if('instanceof object', arr instanceof Object){
alert('arr is a object');
if('instanceof array', arr instanceof Array){
alert('arr is a Array');
}
}else{
alert('this is not a object');
}
Now let's use a simple variable testObj, This is not even a object then how it could be a array,
var testObj;
if('instanceof object', testObj instanceof Object){
alert('testObj is a object');
}else{
alert('testObj is not a object');
}
Try this for more
本文标签: objectHow to judge a javascript variable39s datatype is ArrayStack Overflow
版权声明:本文标题:object - How to judge a javascript variable's datatype is Array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745370463a2655709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论