Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CodeRookie262 opened this issue Jan 29, 2021 · 1 comment
Open

Comments

@CodeRookie262
Copy link
Owner

No description provided.

@CodeRookie262
Copy link
Owner Author

  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
Projects
None yet
Development

No branches or pull requests

1 participant