国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript判斷變量是對象還是數組的方法

2024-05-06 15:46:10
字體:
來源:轉載
供稿:網友

JavaScript判斷變量是對象還是數組的方法,要怎么判斷呢?其實在使用typeof進行判斷的時候一般顯示的返回值為“object”,今天是錯新技術頻道小編分享的方法,希望能幫到您。

typeof都返回object

在JavaScript中所有數據類型嚴格意義上都是對象,但實際使用中我們還是有類型之分,如果要判斷一個變量是數組還是對象使用typeof搞不定,因為它全都返回object

復制代碼 代碼如下:

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);


執行:

?

復制代碼 代碼如下:

?


o typeof is object
a typeof is object


因此,我們只能放棄這種方法,要判斷是數組or對象有兩種方法

?

第一,使用typeof加length屬性

數組有length屬性,object沒有,而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

?

第二,使用instanceof

使用instanceof可以判斷一個變量是不是數組,如:

復制代碼 代碼如下:

var o = { 'name':'lee' };
var a = ['reg','blue'];
?
alert( a instanceof Array );? // true
alert( o instanceof Array );? // false


也可以判斷是不是屬于object

?

復制代碼 代碼如下:

?


var o = { 'name':'lee' };
var a = ['reg','blue'];
?
alert( a instanceof Object );? // true
alert( o instanceof Object );? // true


但數組也是屬于object,所以以上兩個都是true,因此我們要利用instanceof判斷數據類型是對象還是數組時應該優先判斷array,最后判斷object

?

復制代碼 代碼如下:

?


var o = { 'name':'lee' };
var a = ['reg','blue'];
?
var getDataType = function(o){
??? if(o instanceof 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


如果你不優先判斷Array,比如:

?

復制代碼 代碼如下:

?


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


那么數組也會被判斷為object。

錯新技術頻道有大家想要學習的知識,希望閱讀完本文的朋友,在學習這方面知識上,能更進一步。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 建阳市| 永和县| 津南区| 百色市| 哈巴河县| 信宜市| 宁南县| 类乌齐县| 神农架林区| 黄大仙区| 闵行区| 通榆县| 平阳县| 建宁县| 仙居县| 清水河县| 正定县| 宁国市| 霍州市| 康定县| 仲巴县| 射洪县| 苗栗县| 马边| 娱乐| 尖扎县| 湟源县| 黄龙县| 西乌| 洛扎县| 威信县| 太仆寺旗| 含山县| 宁强县| 靖安县| 临沭县| 陆丰市| 德江县| 邛崃市| 衡南县| 民乐县|