Skip to content

你有多少种方法判断一个数据是否属于数组呢? #33

Open

Description

@CodeRookie262
No description provided.

Activity

  1. CodeRookie262 commented on Feb 1, 2021

    @CodeRookie262
    OwnerAuthor
    1. 通过 Array.prototype.isArray 判断一个数据是否属于数组;
    2. 通过 instanceof 判断该数据是否属于Array 实例(缺点:可以修改构造函数上的 Symbol.hasInstance 方法来伪造结果);
    3. 通过 constructor 判断是否是 Array 实例化(只能判断直系构造函数);
    4. 通过 Object.prototype.toString.call 来判断数据是否属于 Array 实例;
    5. 判断该数据的原型链上是否存在 Array.prototype,这个方法和 instanceof 差不多;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions