js文法學習之判斷一個對象是否為數組_javascript技巧

來源:互聯網
上載者:User
1,真正的數組的判斷方法

javascript中最簡單的聲明數組方法為:
var a = [];
判斷是否為數組的最直接的方法為:
複製代碼 代碼如下:

a instanceof Array //true
a .constructor == Array //true

這裡涉及到一個instanceof文法,instanceof是一個雲算符,與"+-*/"一樣,它的文法如下:
result = obj intanceof class
是用來判斷一個對象是否是某個class的一個執行個體,運算結果返回true或者false。javascript中class的定義又是通過建構函式進行初始化的,所以instanceof文法的右操作符class一定是Function的執行個體,即class instanceof Function一定為true,而且如果使用instanceof時右操作符不是Function,就會拋出TypeError異常。所有對象都是Object的執行個體,所以任何對象instanceof Object都返回true。雖然我們說對象都是通過建構函式進行初始化的,但是instanceof卻不是通過檢查對象是否由該函數構造的,而是通過是否由建構函式的prototype繼承來的,下面這個例子可以說明這個問題:
複製代碼 代碼如下:

function Range(low, high) {
this.low = low;
this.high = high;
}
Range.prototype.constructor == Range; //true
Range.prototype = {
include: function(x){ return (x >= this.low && x <= this.high); },
exclude: function(x){ return (x < this.low && x > this.high); }
}
var r = new Range(0, 100);
r instanceof Range; //false
r instanceof Object; //true
Range.prototype.constructor == Objecct; //true

這裡雖然r是通過new Range構造的,但是r卻並不是Range的執行個體,這就是問題所在,Range.prototype指派陳述式覆蓋了預設的建構函式,沒對prototype賦值之前Range.prototype.constructor為Range,賦值之後變成了Object,這也好理解,因為
複製代碼 代碼如下:

Range.prototype = {
include: function(x){ return (x >= this.low && x <= this.high); },
exclude: function(x){ return (x < this.low && x > this.high); }
}

其實等價於:
複製代碼 代碼如下:

Range.prototype = new Object({
include: function(x){ return (x >= this.low && x <= this.high); },
exclude: function(x){ return (x < this.low && x > this.high); }
});

所以Range.prototype.constructor == Object,那麼通過new Range建立出來的執行個體當然就是Object的一個執行個體了。
看官方解釋更直接些:
The instanceof operator does not actually check whether r was initialized by the Range constructor. It checks whether it inherits from Range.prototype.
javascript中還有一個函數typeof具有與instanceof類似的功能,但是它返回的是具體的基礎資料型別 (Elementary Data Type):number,string,function,object,undefined,boolean,只有這六種,不在這六種範圍內的都返回object,也就是說typeof([])返回的是object,而不是array。
另一個涉及到的文法是constructor,constructor返回對象的建構函式:
複製代碼 代碼如下:

var a = [];
a.constructor; //Array

建構函式是一個對象的初始化函數,採用new調用,如果對象是一個Array,那麼其constructor應該就是Array,自己寫的類就不一定了,因為可能會吧prototype中的constructor更改掉。

2,偽數組的判斷方法

javascript中有一種偽數組,它可以使用類似於Array的遍曆方法進行遍曆,有length屬性擷取元素的長度,可以使用[]下標來擷取指定的元素,這種對象我們稱之為偽數組,JQuery中的對象就是典型的偽數組,如下圖:
 
所以判斷是否是偽數組的關鍵就是判斷是否有length屬性,是否存在基本的數組操作函數splice,下面就是判斷方法:
複製代碼 代碼如下:

var is_array = function(value) {
return value &&
typeof value === 'object' &&
typeof value.length === 'number' &&
typeof value.splice === 'function' &&
!(value.propertyIsEnumerable('length'));
};

這裡propertyIsEnumerable就是用來判斷length屬性是否可列舉,其實原生的String對象也是有類似Array的效果,但是我們不能把它當作Array對象,所以這裡需要判斷typeof value == "object",因為typeof一個String對象,返回的是string。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.