3 回答

慕娘9325324
TA貢獻1783條經(jīng)驗 獲得超4個贊
在JavaScript中所有數(shù)據(jù)類型嚴格意義上都是對象,但實際使用中我們還是有類型之分,如果要判斷一個變量是數(shù)組還是對象使用typeof搞不定,因為它全都返回object
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
添加回答
舉報
0/150
提交
取消