Ⅰ js里判斷變數是數組還是對象的四種方法
因為無論是數組還是對象,對於typeof的操作返回值都為object,所以就有了區分數組類型和對象類型的需要:
方一:通過length屬性:一般情況下對象沒有length屬性值,其值為undefiend,而數組的length值為number類型
缺點:非常不實用,當對象的屬性存在length,且其值為number(比如類數組),則該方法失效,不建議使用,看看即可。
*方二:通過instanceof來判斷區分
var arr = [1, 2, 3]; var obj = {name: 'lyl',age: 18, 1: 'name'}console.log(arr instanceof Array); //trueconsole.log(obj instanceof Array); //false
*方三:通過constructor
var arr = [1, 2, 3]; var obj = {name: 'lyl',age: 18, 1: 'name'}console.log(arr.constructor === Array); //trueconsole.log(obj.constructor === Array); //false
Ⅱ 01 JS的數據類型及如何判斷數據類型
在 ES5 中,存在 6 種數據類型。
基本類型(值類型): String、Number、Boolean、undefined、null
對象類型(引用類型):Object,其中 Object 有三種特殊的對象(Function、Array、Date)
PS: undefined 和 null 的第一個字母都是小寫
Q: undefined 和 null 有什麼區別?
A: 當一個變數被聲明,但是還沒有賦值的時候,則為 undefined.
當一個變數被聲明了,並且已經賦值了,賦的值為 null,則為 null
根本區別就是在於在聲明的時候有沒有被賦值
常用的判斷數據類型的方法
劃重點!!! typeof(xxx) 返回的是一個字元串
舉個栗子1
舉個栗子2
舉個栗子3
typeof 可以判斷以上 4 中基本類型(Number、String、Boolean、undefined)
判斷不了 null 類型
舉個栗子4
是不是很奇怪?null 作為基礎類型,但是在判斷類型的時候卻為 object?
其實設計者是這么想的,他先定義了一個變數,這個變數是准備賦值為對象的,由於某些原因,比如對象的屬性還不清楚,所以一開始就給變數賦值為 null,表明這個變數將來是個對象。
null 還有另外一個作用,在最後的時候,給變數賦值為 null,可以讓變數指向的對象成為垃圾對象,從而被垃圾回收器回收。
上面的例子證明,null 類型和 對象類型通過 typeof 是無法區分的。
沒錯,JS 的開發者就是不想讓你們這么好過,只記住一個規矩就想闖天下了嗎??? Naive!!!
為了讓你們多學點本領,於是就有了 instanceof
instanceof 字面意思就是實例。a instanceof b, a 是 b 的實例,b 為 構造函數 。返回值為 布爾值
實現原理是通過檢測 b.prototype 是否存在於 a 的原型鏈上
舉個栗子1
但是吧,你想用來判斷 null 類型,不好意思,直接報錯, null 並不是一個對象
先看完下面的例子,再來看怎麼判斷 null
下面看個有迷惑行為的例子
言歸正傳,怎麼說來說去都沒說怎麼判斷是不是 null 類型
===
直接上全等於 === 不就好了嘛,就是這么簡單快捷!!!
typeof:
可以判斷除了 null 之外的值類型 Number、String、Boolean、undefined
還可以判斷一個引用類型 Function
不能區分: null 和 Object
instanceof:
判斷對象的具體類型
===
判斷 undefined 和 null
一般情況下,使用 typeof 去判斷就可以了。
當確定是 Object 類型的數據,則使用 instanceof 去具體區分是屬於 Function/Array/Date 的哪種類型
很多情況下,都要判斷數據不能為 undefined 和 null,那就可以直接判斷 xxx !== undefined &&& xxx !== null
Ⅲ js查看數據是什麼類型
可以使用typeof查看數據類型。
typeof返回數據類型為字元串,根據字元串就可以知道具體的類型,如:number為數字類型。
Ⅳ js 如何判斷變數的數據類型
var k,k1;
k=0;
k1="0";
alert(Object.prototype.toString.apply(k));
alert(Object.prototype.toString.apply(k1));
輸出結果分別為[object number]
[object string]
Ⅳ js怎麼彈出變數的數據類型
通過type of 變數名的方式獲取變數的數據類型。
因為js變數是鬆散類型(即弱類版型)的,可以用來權保存任何類型的數據,所以用typeof 用來檢測給定變數的數據類型,可能的返回值有:
1. 'undefined' --- 這個值未定義;
2. 'boolean' --- 這個值是布爾值;
3. 'string' --- 這個值是字元串;
4. 'number' --- 這個值是數值;
5. 'object' --- 這個值是對象或null;
6. 'function' --- 這個值是函數。
舉例:
var aa = 'test string';
alert(typeof aa); // 'string'
alert(typeof 90); // 'number'
Ⅵ js 中一個變數是什麼類型
js中變數類型是由後面的值確定的,以下教大家如何知道當前變數的類型。具體方法如下。
Ⅶ js如何判斷變數的數據類型
檢測簡單抄的數據類型的方法
typeof方法用於檢測簡單的數據類型如typeof 12
instanceof的實例方法檢測如[] instanceof Array // true
arr.constructor == Array判斷arr的構造函數是否為數組,如果是則arr是數組
Array.isArray([])判斷是否是數組
精確判斷數據類型Object.prototype.toString.call(arr)
Ⅷ 如何獲取JS變數類型
如何判斷js中的數據類型:typeof、instanceof、 constructor、 prototype方法比較
如何判斷js中的類型呢,先舉幾個例子:
var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e =
function(){alert(111);};
var f =
function(){this.name="22";};
最常見的判斷方法:typeof
alert(typeof a)
------------> string
alert(typeof b)
------------> number
alert(typeof c)
------------> object
alert(typeof d)
------------> object
alert(typeof e)
------------> function
alert(typeof f)
------------> function
其中typeof返回的類型都是字元串形式,需注意,例如:
alert(typeof a == "string")
-------------> true
alert(typeof a == String)
---------------> false
另外typeof
可以判斷function的類型;在判斷除Object類型的對象時比較方便。
判斷已知對象類型的方法: instanceof
alert(c instanceof Array)
---------------> true
alert(d instanceof
Date)
alert(f instanceof Function)
------------> true
alert(f instanceof function)
------------> false
注意:instanceof
後面一定要是對象類型,並且大小寫不能錯,該方法適合一些條件選擇或分支。
根據對象的constructor判斷:
constructor
alert(c.constructor ===
Array) ----------> true
alert(d.constructor === Date)
-----------> true
alert(e.constructor ===
Function) -------> true
注意: constructor 在類繼承時會出錯
eg,
function A(){};
function B(){};
A.prototype = new B(); //A繼承自B
var aObj = new A();
alert(aobj.constructor === B) ----------->
true;
alert(aobj.constructor === A) ----------->
false;
而instanceof方法不會出現該問題,對象直接繼承和間接繼承的都會報true:
alert(aobj instanceof B) ---------------->
true;
alert(aobj instanceof B) ---------------->
true;
言歸正傳,解決construtor的問題通常是讓對象的constructor手動指向自己:
aobj.constructor = A;
//將自己的類賦值給對象的constructor屬性
alert(aobj.constructor === A) ----------->
true;
alert(aobj.constructor === B) ----------->
false; //基類不會報true了;
通用但很繁瑣的方法: prototype
alert(Object.prototype.toString.call(a) === 『[object String]』)
-------> true;
alert(Object.prototype.toString.call(b) === 『[object Number]』)
-------> true;
alert(Object.prototype.toString.call(c) === 『[object Array]』)
-------> true;
alert(Object.prototype.toString.call(d) === 『[object Date]』)
-------> true;
alert(Object.prototype.toString.call(e) === 『[object Function]』)
-------> true;
alert(Object.prototype.toString.call(f) === 『[object Function]』)
-------> true;
大小寫不能寫錯,比較麻煩,但勝在通用。
通常情況下用typeof
Ⅸ Javascript使用什麼方法可以獲得變數的數據類型
用typeof(var) 來返態族回一個數據類型x0dx0a返回的可能值有x0dx0a'undefined- 如果變數是 Undefined 類型的x0dx0a'boolean- 如果變數是 Boolean 類型的x0dx0a'number- 如果變數是蘆嘩 Number 類型的x0dx0a'string- 如果變數是 String 類型的x0dx0a'object- 如果變數是帆嘩弊一種引用類型或 Null 類型的x0dx0ax0dx0aif(typeof(123)=='number')alert('this is a number')x0dx0a以此類推
Ⅹ Javascript如何判斷一個變數是普通變數還是數組還是對象
1、使用typeof操作符檢測變數類型
數組、Null、Object
為
object
類型
字元串
為
string
類型
true和false
為
boolean
類型
整型、浮點型為
number
類型
2、如果要區分數組和非數組對象,需要使用構造函數來判斷
if(arr.constructor==Array)
//
arr
是數組
else
//
arr
不是數組