javascript
JavaScript判断一个变量是对象还是数组
typeof都返回object
在JavaScript中所有數(shù)據(jù)類型嚴(yán)格意義上都是對(duì)象,但實(shí)際使用中我們還是有類型之分,如果要判斷一個(gè)變量是數(shù)組還是對(duì)象使用typeof搞不定,因?yàn)樗挤祷豲bject
| 1 2 3 4 5 6 | 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); |
執(zhí)行:
o typeof is object
a typeof is object ??
因此,我們只能放棄這種方法,要判斷是數(shù)組or對(duì)象有兩種方法
第一,使用typeof加length屬性
數(shù)組有l(wèi)ength屬性,object沒有,而typeof數(shù)組與對(duì)象都返回object,所以我們可以這么判斷
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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可以判斷一個(gè)變量是不是數(shù)組,如:
| 1 2 3 4 5 | var o = { 'name':'lee' }; var a = ['reg','blue']; alert( a instanceof Array );? // true alert( o instanceof Array );? // false |
也可以判斷是不是屬于object
| 1 2 3 4 5 | var o = { 'name':'lee' }; var a = ['reg','blue']; alert( a instanceof Object );? // true alert( o instanceof Object );? // true |
但數(shù)組也是屬于object,所以以上兩個(gè)都是true,因此我們要利用instanceof判斷數(shù)據(jù)類型是對(duì)象還是數(shù)組時(shí)應(yīng)該優(yōu)先判斷array,最后判斷object
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 |
如果你不優(yōu)先判斷Array,比如:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 |
那么數(shù)組也會(huì)被判斷為object。
?
定義和用法
length 屬性可設(shè)置或返回?cái)?shù)組中元素的數(shù)目。
語(yǔ)法
arrayObject.length說明
數(shù)組的 length 屬性總是比數(shù)組中定義的最后一個(gè)元素的下標(biāo)大 1。對(duì)于那些具有連續(xù)元素,而且以元素 0 開始的常規(guī)數(shù)組而言,屬性 length 聲明了數(shù)組中的元素的個(gè)數(shù)。
數(shù)組的 length 屬性在用構(gòu)造函數(shù) Array() 創(chuàng)建數(shù)組時(shí)被初始化。給數(shù)組添加新元素時(shí),如果必要,將更新 length 的值。
設(shè)置 length 屬性可改變數(shù)組的大小。如果設(shè)置的值比其當(dāng)前值小,數(shù)組將被截?cái)?#xff0c;其尾部的元素將丟失。如果設(shè)置的值比它的當(dāng)前值大,數(shù)組將增大,新的元素被添加到數(shù)組的尾部,它們的值為 undefined。
?
轉(zhuǎn)載于:https://www.cnblogs.com/loewe0202/p/5657239.html
總結(jié)
以上是生活随笔為你收集整理的JavaScript判断一个变量是对象还是数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最新跨年文艺句子文案朋友圈262个
- 下一篇: JSP的改动需要重启应用服务器才能生效?