如何判断一个变量在JS中是否为数组?
- 内容介绍
- 文章标签
- 相关推荐
本文共计987个文字,预计阅读时间需要4分钟。
在开发过程中,我们经常遇到需要判断变量是否为数组类型的情况。以下是将常用判断变量是否为数组的几种方法整理如下:
1. 常用方法 1. `Object.prototype.toString.call()`
具体实现如下:javascript// 判断变量是否为数组function isArray(obj) { return Object.prototype.toString.call(obj)==='[object Array]';}
// 示例var arr=[1, 2, 3];console.log(isArray(arr)); // 输出:true
日常开发中,我们经常遇到这种情况,需要我们判断变量是否是一个数组类型。
那么今天我把常用的判断变量是否是数组类型的方法,整理在这里:
一、常用方法
1. Object.prototype.toString
通常我们可以使用Object.prototype.toString方法进行判断,详细可以查看《Object.prototype.toString() - MDN - Mozilla》。
let arr = [1,2,3], obj = {name: "leo"};function isArray(a){
return Object.prototype.toString.call(a)==='[object Array]';
}
isArray(arr); // true
isArray(obj); // false
2. Array.isArray(arr)
Array.isArray(arr)方法使用起来很简单:
let arr = [1,2,3], obj = {name: "leo"};Array.isArray(arr); // true
Array.isArray(obj); // false
需要注意的是Array.isArray(arr)方法虽然简单,但是存在兼容性问题,Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题。
if(!Array.isArray){Array.isArray = function(a){
return Object.prototype.toString.call(a)==='[object Array]'
}
}
二、不够准确的方法
1. 原型链
使用原型链判断也比较简单:
arr.__proto__.constructor === Array; // true
obj.__proto__.constructor === Array; // true
arr.constructor === Array; //true
obj.constructor === Array; //true
但是之所以说constructor不够准确,是因为在不同执行环境下,constructor判断会不正确。
比如下面这种情况,我们重写constructor:
arr.constructor === Array // true
obj.constructor === Array // false
obj.constructor = Array;
obj.constructor === Array // true
2.instanceof
简单使用instanceof如下:
let arr = [1,2,3], obj = {name: "leo"};arr instanceof Array; // true
obj instanceof Array; // false
但是instanceof也存在局限性,它必须在当前页面声明,如父页面中存在一个 iframe,并且 iframe 中引用了一个子页面,在子页面中声明了一个arr,并将其赋值给父页面的一个变量,这时判断该变量,Array == object.constructor;会返回 false;
let iframe = document.createElement('iframe');document.body.appendChild(iframe);
let arr = [1,2,3];
myArray = window.frames[0].Array;
let myArr = new myArray(4,5,6);
myArr instanceof Array; //false
myArr.constructor == Array;// false
Array.prototype == myArray.prototype); //false
arr instanceof myArray); //false
myArr.constructor === Array; // false
arr.constructor === Array; // true
myArr.constructor === myArray;// true
Array.isArray(arrx); //true
三、错误的方法
1. typeof
typeof是无法判断是否是数组的:
let arr = [1,2,3], obj = {name: "leo"};typeof arr; // "object"
typeof obj; // "object"
所以可以看出,typeof适合用来判断基本数据类型。
typeof的一些判断结果:
// 基本类型typeof 123; //number
typeof "leo"; //string
typeof true; //boolean
typeof undefined; //undefined
typeof null; //object
let leo = Symbol;
typeof leo; //symbol
// 引用类型
typeof [1,2,3]; //object
typeof {}; //object
typeof function(){}; //function
typeof Array; //function Array类型的构造函数
typeof Object; //function Object类型的构造函数
typeof Symbol; //function Symbol类型的构造函数
typeof Number; //function Number类型的构造函数
typeof String; //function String类型的构造函数
typeof Boolean; //function Boolean类型的构造函数
四、总结
本文主要给大家从三个角度去介绍一些判断变量是否是数组的方法,在日常开发中中的 2 个方法,已经足够我们使用了,也建议使用这 2 种方法。
参考文章
《判断是否是数组的几种方法》
▼
本文共计987个文字,预计阅读时间需要4分钟。
在开发过程中,我们经常遇到需要判断变量是否为数组类型的情况。以下是将常用判断变量是否为数组的几种方法整理如下:
1. 常用方法 1. `Object.prototype.toString.call()`
具体实现如下:javascript// 判断变量是否为数组function isArray(obj) { return Object.prototype.toString.call(obj)==='[object Array]';}
// 示例var arr=[1, 2, 3];console.log(isArray(arr)); // 输出:true
日常开发中,我们经常遇到这种情况,需要我们判断变量是否是一个数组类型。
那么今天我把常用的判断变量是否是数组类型的方法,整理在这里:
一、常用方法
1. Object.prototype.toString
通常我们可以使用Object.prototype.toString方法进行判断,详细可以查看《Object.prototype.toString() - MDN - Mozilla》。
let arr = [1,2,3], obj = {name: "leo"};function isArray(a){
return Object.prototype.toString.call(a)==='[object Array]';
}
isArray(arr); // true
isArray(obj); // false
2. Array.isArray(arr)
Array.isArray(arr)方法使用起来很简单:
let arr = [1,2,3], obj = {name: "leo"};Array.isArray(arr); // true
Array.isArray(obj); // false
需要注意的是Array.isArray(arr)方法虽然简单,但是存在兼容性问题,Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题。
if(!Array.isArray){Array.isArray = function(a){
return Object.prototype.toString.call(a)==='[object Array]'
}
}
二、不够准确的方法
1. 原型链
使用原型链判断也比较简单:
arr.__proto__.constructor === Array; // true
obj.__proto__.constructor === Array; // true
arr.constructor === Array; //true
obj.constructor === Array; //true
但是之所以说constructor不够准确,是因为在不同执行环境下,constructor判断会不正确。
比如下面这种情况,我们重写constructor:
arr.constructor === Array // true
obj.constructor === Array // false
obj.constructor = Array;
obj.constructor === Array // true
2.instanceof
简单使用instanceof如下:
let arr = [1,2,3], obj = {name: "leo"};arr instanceof Array; // true
obj instanceof Array; // false
但是instanceof也存在局限性,它必须在当前页面声明,如父页面中存在一个 iframe,并且 iframe 中引用了一个子页面,在子页面中声明了一个arr,并将其赋值给父页面的一个变量,这时判断该变量,Array == object.constructor;会返回 false;
let iframe = document.createElement('iframe');document.body.appendChild(iframe);
let arr = [1,2,3];
myArray = window.frames[0].Array;
let myArr = new myArray(4,5,6);
myArr instanceof Array; //false
myArr.constructor == Array;// false
Array.prototype == myArray.prototype); //false
arr instanceof myArray); //false
myArr.constructor === Array; // false
arr.constructor === Array; // true
myArr.constructor === myArray;// true
Array.isArray(arrx); //true
三、错误的方法
1. typeof
typeof是无法判断是否是数组的:
let arr = [1,2,3], obj = {name: "leo"};typeof arr; // "object"
typeof obj; // "object"
所以可以看出,typeof适合用来判断基本数据类型。
typeof的一些判断结果:
// 基本类型typeof 123; //number
typeof "leo"; //string
typeof true; //boolean
typeof undefined; //undefined
typeof null; //object
let leo = Symbol;
typeof leo; //symbol
// 引用类型
typeof [1,2,3]; //object
typeof {}; //object
typeof function(){}; //function
typeof Array; //function Array类型的构造函数
typeof Object; //function Object类型的构造函数
typeof Symbol; //function Symbol类型的构造函数
typeof Number; //function Number类型的构造函数
typeof String; //function String类型的构造函数
typeof Boolean; //function Boolean类型的构造函数
四、总结
本文主要给大家从三个角度去介绍一些判断变量是否是数组的方法,在日常开发中中的 2 个方法,已经足够我们使用了,也建议使用这 2 种方法。
参考文章
《判断是否是数组的几种方法》
▼

