站长资讯网
最全最丰富的资讯网站

es6怎么判断一个变量是否为数组

判断方法:1、利用Array对象的isArray()函数来判断变量是不是数组,语法“Array.isArray(obj)”,如果是数组返回true,否则返回false。2、通过“Object.prototype.toString()”来判断,语法“Object.prototype.toString.apply(a) === '[object Array]'”。

es6怎么判断一个变量是否为数组

本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。

JS的弱类型机制导致判断变量类型是初级前端开发者面试时的必考题,一般我都会将其作为考察候选人第一题,然后基于此展开。

在ES5中判断变量是否为数组

在ES5中,我们至少有如下5种方式去判断一个值是否数组:

var a = [];  // 1.基于instanceof  a instanceof Array;  // 2.基于constructor  a.constructor === Array;  // 3.基于Object.prototype.isPrototypeOf  Array.prototype.isPrototypeOf(a);  // 4.基于getPrototypeOf  Object.getPrototypeOf(a) === Array.prototype;  // 5.基于Object.prototype.toString  Object.prototype.toString.apply(a) === '[object Array]';
登录后复制

以上,除了Object.prototype.toString外,其它方法都不能正确判断变量的类型。

要知道,代码的运行环境十分复杂,一个变量可能使用浑身解数去迷惑它的创造者。且看:

var a = {     __proto__: Array.prototype };  // 分别在控制台试运行以下代码  // 1.基于instanceof  a instanceof Array; // => true  // 2.基于constructor  a.constructor === Array; // => true  // 3.基于Object.prototype.isPrototypeOf  Array.prototype.isPrototypeOf(a); // => true  // 4.基于getPrototypeOf  Object.getPrototypeOf(a) === Array.prototype; // => true
登录后复制

以上,4种方法将全部返回true,为什么呢?我们只是手动指定了某个对象的__proto__属性为Array.prototype,便导致了该对象继承了Array对象,这种毫不负责任的继承方式,使得基于继承的判断方案瞬间土崩瓦解。

不仅如此,我们还知道,Array是堆数据,变量指向的只是它的引用地址,因此每个页面的Array对象引用的地址都是不一样的。iframe中声明的数组,它的构造函数是iframe中的Array对象。如果在iframe声明了一个数组x,将其赋值给父页面的变量y,那么在父页面使用y instanceof Array ,结果一定是false的。而最后一种返回的是字符串,不会存在引用问题。实际上,多页面或系统之间的交互只有字符串能够畅行无阻。

在ES6中判断变量是否为数组

鉴于数组的常用性,在ES6中新增了Array.isArray方法,使用此方法判断变量是否为数组,则非常简单,如下:

Array.isArray([]); // => true  Array.isArray({0: 'a', length: 1}); // => false
登录后复制

实际上,通过Object.prototype.toString去判断一个值的类型,也是各大主流库的标准。因此Array.isArray的polyfill通常长这样:

if (!Array.isArray){      Array.isArray = function(arg){          return Object.prototype.toString.call(arg) === '[object Array]';      };  }
登录后复制

赞(0)
分享到: 更多 (0)