判断数组的方法你知道哪些?

ECMAScript中有5种简单数据类型(也称为基本数据类型):Undefined、Null、Boolean、Number和String,ES6又新提出一种Symbol类型,也是基本类型。还有1种复杂数据类型:Object。我们前面说过typeof和instanceof的区别,我们都知道typeof只能检测出number、boolean、string、function、object、undefined以及symbol这些类型,对于Array、Object、Null等都只能返回Object。那么如何来辨别数组呢?接下来去我们展开讨论。

方法1:instanceof

instanceof 的内部机制是通过判断对象的原型链中是不是能找到类型的 prototype。

使用instanceof判断一个对象是否为数组,instanceof会判断这个对象的原型链上是否会找到对应的Array的原型,找到返回 true,否则返回 false。

用来检测引用类型是Array/Function/Object,无法检测基本类型

123 instanceof Number; // false
'aaa' instanceof String; // false
true instanceof Boolean; // false
[]  instanceof Array; // true
{} instanceof Object; // true
function(){} instanceof Function; // true

但 instanceof 只能用来判断引用类型是Array/Function/Object,基本类型不可以。并且所有对象类型 instanceof Object 都是 true。

[]  instanceof Object; // true

方法2:constructor

constructor 属性返回对创建此对象的数组函数的引用,就是返回对象相对应的构造函数。

("aaa").constructor == String; // true
(123).constructor == Number;  // true
(true).constructor === Boolean; // true
(function() {}).constructor === Function); // true
([]).constructor == Array;  // true
({}).constructor == Object;  // true

但是如果创建的对象更改了原型,是无法检测到最初的类型。

方法3:__proto__
也是通过原型去判断;

[1,2,3].__proto__  === Array.prototype; // true

方法4:Object.prototype.toString

每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型。但当除了 Object 类型的对象外,其他类型直接使用 toString 方法时,会直接返回都是内容的字符串,所以我们需要使用call或者apply方法来改变toString方法的执行上下文。

const arr = ['a','b', 'c'];
arr.toString(); // "a,b,c"
Object.prototype.toString.call(arr); // "[object Array]"

这种方法对于所有基本的数据类型都能进行判断,即使是 null 和 undefined。

Object.prototype.toString.call('aaa'); // "[object String]"
Object.prototype.toString.call(123); // "[object Number]"
Object.prototype.toString.call(Symbol(1)); // "[object Symbol]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(function(){}); // "[object Function]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call({}); // "[object Object]"

Object.prototype.toString.call() 常用于判断浏览器内置对象时。

方法5:Array.isArray

Array.isArray是es6新增的方法,用来判断对象是否为数组。

扫描二维码关注公众号,回复: 8482807 查看本文章
Array.isArray([]) //true
  • instanceof 与 isArray

    当检测Array实例时,Array.isArray 优于 instanceof ,因为 Array.isArray 可以检测出 iframes
    javascript 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 Object.prototype.toString.call(arr); // true // Considered harmful, because doesn't work though iframes arr instanceof Array; // false
  • Array.isArray() 与 Object.prototype.toString.call()

    Array.isArray()是ES5新增的方法,当不存在 Array.isArray() ,可以用 Object.prototype.toString.call() 实现。

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

参考:https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/23

猜你喜欢

转载自www.cnblogs.com/QueenZhang/p/12176734.html
今日推荐