判斷數(shù)組的方式

typeof 方法

檢測一個變量的類型(并不能判斷是否是數(shù)組)
typeof是一個運算符,有兩種使用方式:typeof(表達式);typeof 變量名,一種是對表達式做運算,一種是對變量名做運算。
typeof 的返回值的類型為字符串,有以下幾種:
1、'undefined' —— 未定義的變量或值
2、'boolean' —— 布爾類型的值或變量
3、'string' —— 字符串類型的變量或值
4、'number' —— 數(shù)字類型的變量或值
5、'object' —— 對象類型的變量或值,或者 null(這個是 js 歷史遺留問題,將 null 作為 object 類型處理)
6、'function' —— 函數(shù)類型的變量或值
簡單示例:

console.log(typeof a); // 'undefined'
console.log(typeof true); // 'boolean'
console.log(typeof '123'); // 'string'
console.log(typeof 123); // 'number'
console.log(typeof NaN); // 'number'
console.log(typeof null); // 'object'
var obj = new String();
console.log(typeof obj); // 'object'
var fn = function(){};
console.log(typeof fn); // 'function'
console.log(typeof class c{}); // 'function'

typeof 運算符用于判斷對象的類型,但是對于一些創(chuàng)建的對象,它都會返回 object,下面看看數(shù)組的判斷方式。

1、Array.isArray

ES5 新增的方法

const arr = [1, 2, 3, 4, 5];
Array.isArray(arr); // true
instanceof

一般來說,instanceof 關鍵字,用于判斷某個元素是否某對象構造函數(shù)實例。(判斷右邊參數(shù)的原型是否在左邊參數(shù)的原型鏈上。)

const arr = [1, 2, 3, 4, 5]
arr instanceof Array; // true
3、toString

每一個繼承 Object 的對象都有 toString 方法,如果 toString 方法沒有重寫,會返回 [object type],其中 type 為對象的類型。但當除了 object 類型的對象外,其他類型直接使用 toString 方法,會直接返回內容的字符串,所以我們需要用 call 或者 apply 方法來改變 toString 方法的執(zhí)行上下文。

let arr = ['hello', 'world'];
arr.toString(); // 'hello world'
Object.prototype.toString.call(arr); // '[object Array]'

一般來說這種方式的判定是各大庫的一種 Array.isArray 的代替實現(xiàn)。
例如:polyfill 中就是如此實現(xiàn):call 方法用來改變 toString 方法的執(zhí)行上下文。

if (!Array.isArray) {
    Array.isArray = function (arg) {
        return Object.prototype.toString.call(arg) === '[object Array]'
    }
}

這種方法對于所有基本數(shù)據(jù)類型都可以進行判斷,包括 null 和 undefined

Object.prototype.toString.call('yue'); // '[object String]'
Object.prototype.toString.call(1); // '[object Number]'
Object.prototype.toString.call(Symbol(1)); // '[object Symbol]'
Object.prototype.toString.call(null); // '[objectNull]'
Object.prototype.toString.call(undefined); // '[object Undefined]'
Object.prototype.toString.call(function(){}); // '[objectFunction]'
Object.prototype.toString.call({name: 'yue'}); // '[object Object]'
const arr = [1, 2, 3, 4, 5];
Object.prototype.toString.call(arr) === '[object Array]' // true
4、constructor

通過構造函數(shù)來判定:

const arr = [1, 2, 3, 4, 5];
arr.constructor === Array; // true
arr.__proto__.constructor === Array; // true
弊端

instanceof 和 constructor 的判定也存在一些弊端,他們判定的數(shù)組必須定義在同一個頁面,否則就會判定為 false。
如果在 iframe 中的數(shù)組判定,就會出錯:

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]
 
// Correctly checking for Array
Array.isArray(arr); // true
// Considered harmful, because doesn't work through iframes
arr instanceof Array; // false
arr.constructor === Array; // false
總結

由以上判定方法可以得出,polyfill 的判定方式是最合理的,也是最具有兼容性的一種判定:利用 toString 判定。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容