前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面试官:怎么判断数组类型?

面试官:怎么判断数组类型?

原创
作者头像
前端老道
发布2022-09-22 18:39:52
6690
发布2022-09-22 18:39:52
举报
WechatIMG1517.jpeg
WechatIMG1517.jpeg

前端面试过程中,面试官常常会问到怎么判断数组?判断数组的方法常见的有以下几种:

1. Object.prototype.toString.call()
  Object.prototype.toString.call([1,2,3,4]) === “[object Array]” 
  true

当然此方法也可以判断对象,字符串等任意变量的数据类型

2. 原型链判断数组
let a = [1,2,3]
a.__proto__ === Array.prototype
true
3. ES6方法 Array.isArray
let a = [1,2,3]
Array.isArray(a)
true
4. instanceof 判断
// instanceof 运算符用于验证构造函数的 prototype 属性是否出现在对象的原型链中的任意位置

let a = [1,2,3]
a instanceof Array
5.通过constructor判断
// 实例的构造函数属性constructor指向构造函数,通过constructor属性可以判断是否为一个数组

let a = [1,2,3]
a.constructor === Array
true
6. 通过 Array.prototype.isPrototypeOf
//  isPrototypeOf用于判断 一个对象是否是另一个对象的原型
//  只要调用者在传入对象的原型链上都会返回 true
  
let a = [1,2,3]
Array.prototype.isPrototypeOf(a)
true

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Object.prototype.toString.call()
  • 2. 原型链判断数组
  • 3. ES6方法 Array.isArray
  • 4. instanceof 判断
  • 5.通过constructor判断
  • 6. 通过 Array.prototype.isPrototypeOf
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档