当前位置:网站首页>判断变量是否为数组
判断变量是否为数组
2022-07-05 13:57:00 【前端西瓜哥】
大家好,我是前端西瓜哥,今天带大家学习在 JS 中如何判断一个对象是否为数组。
Array.isArray
最好的写法是使用 Array.isArray(val)
。
因为该方法能正确判断 iframe 传过来的变量。
instanceof
val instanceof Array
对于一个普通数组,它的原型链是这样的:
数组实例 -> Array.protype -> Object.prototype -> null
如果一个变量是数组,那它的原型链上就一定会有 Array.prototype。
instanceof 的原理其实就左值沿着它的原型链,和右侧值的 prototype 属性比较,看是否相等。找到相等的就返回 true,否则返回 false。
但 instanceof 无法处理 iframe 的变量,因为 iframes 的 Array 并不是当前页面的 Array,它们指向各自的内存空间。
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const ArrayInFrame = window.frames[0].Array; // iframe中的构造函数
const arr = new ArrayInFrame();
arr instanceof Array // false
Array.isArray(arr); // true
只要是能判断 对象是否在原型链上 的方法都是可以的用来判断是否为数组的,所以你还可以用:
// isPrototypeOf 方法
// Array.prototype 是否在 val 的原型链上
Array.prototype.isPrototypeOf(val);
// getPrototypeOf 方法
// val 的上一个原型是否为 Array.prototype
Object.getPrototypeOf(val) === Array.prototype
Object.prototype.toString.call
Object.prototype.toString.call(val) === '[object Array]';
对象都可以通过 toString 方法生成一个字符串,对于对象,通常会返回 [object Xxx]
的形式的字符串,对于数组,则会返回 [object Array]
。
结尾
考虑到要兼容 iframe 的变量,所以建议使用 Array.isArray 方案。
此外判断数组还可以使用查找原型链对象的方法,以及通过 toString 得到的字符串的方法。
我是前端西瓜哥,欢迎关注我,掌握更多前端面试题。
边栏推荐
- Implementation process of WSDL and soap calls under PHP5
- IP packet header analysis and static routing
- 2022 driller (drilling) examination question bank and simulation examination
- Idea remote debugging agent
- matlab学习2022.7.4
- Zhubo Huangyu: these spot gold investment skills are not really bad
- WebRTC的学习(二)
- Attack and defense world web WP
- 2022 construction welder (special type of construction work) special operation certificate examination question bank and online simulation examination
- [machine learning notes] several methods of splitting data into training sets and test sets
猜你喜欢
Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
【公开课预告】:视频质量评价基础与实践
These 18 websites can make your page background cool
Elfk deployment
常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
Data Lake (VII): Iceberg concept and review what is a data Lake
uplad_ Labs first three levels
明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
神经网络物联网未来发展趋势怎么样
OSI and tcp/ip protocol cluster
随机推荐
链表(简单)
Ordering system based on wechat applet
神经网络物联网未来发展趋势怎么样
zabbix 监控
Hide Chinese name
About the problem and solution of 403 error in wampserver
Liste des liens (simple)
牛客网:拦截导弹
物联网应用技术专业是属于什么类
UE源码阅读[1]---由问题入手UE中的延迟渲染
UE source code reading [1]--- starting with problems delayed rendering in UE
Deep copy is hard
[public class preview]: basis and practice of video quality evaluation
Brief introduction to revolutionary neural networks
Solve the problem of invalid uni app configuration page and tabbar
瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
Financial one account Hong Kong listed: market value of 6.3 billion HK $Ye wangchun said to be Keeping true and true, long - term work
Convolutional Neural Networks简述
asp.net 读取txt文件
[South China University of technology] information sharing of postgraduate entrance examination and re examination