Typeof returns object
All data types in JavaScript are strictly objects, but in actual use we still have different types. If you want to determine whether a variable is an array or an object, it cannot be handled with typeof, because it all returns object
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
document.write( ' o typeof is ' + typeof o);
document.write( ' <br />');
document.write( ' a typeof is ' + typeof a);
implement:
The code copy is as follows:
o typeof is object
a typeof is object
Therefore, we can only give up this method. We must judge that there are two methods for array or object.
First, use typeof to add length attribute
The array has a length attribute, object does not, and the typeof array and object both return object, so we can judge this way
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(typeof o == 'object'){
if( typeof o.length == 'number' ){
return 'Array';
}else{
return 'Object';
}
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
Second, use instanceof
Use instanceof to determine whether a variable is an array, such as:
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Array); // true
alert( o instanceof Array); // false
You can also determine whether it belongs to an object
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
alert( a instanceof Object ); // true
alert( o instanceof Object ); // true
But the array also belongs to object, so both of the above are true. Therefore, when we use instanceof to determine whether the data type is an object or an array, we should first judge the array, and finally judge the object
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instance of Array){
return 'Array'
}else if( o instanceof Object ){
return 'Object';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
If you don't prioritize Array, for example:
The code copy is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(o instanceof Object){
return 'Object'
}else if( o instanceof Array ){
return 'Array';
}else{
return 'param is no object type';
}
};
alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Object
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type
Then the array will also be judged as object.