1. 程式人生 > >從javascript判斷一個物件是否為陣列中學習js語法

從javascript判斷一個物件是否為陣列中學習js語法

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類似的功能,但是它返回的是具體的基本資料型別: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。