当前位置:网站首页>判断变量是否为数组
判断变量是否为数组
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 得到的字符串的方法。
我是前端西瓜哥,欢迎关注我,掌握更多前端面试题。
边栏推荐
- Laravel framework operation error: no application encryption key has been specified
- Xampp configuring multiple items
- 链表(简单)
- Network security - Novice introduction
- Network security HSRP protocol
- When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
- 广发期货排名多少?网上办理广发期货开户安全可靠吗?
- 金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功
- Requset + BS4 crawling shell listings
- Those things I didn't know until I took the postgraduate entrance examination
猜你喜欢

【华南理工大学】考研初试复试资料分享

物联网应用技术专业是属于什么类

OSI and tcp/ip protocol cluster

The development of speech recognition app with uni app is simple and fast.

PHP basic syntax

Embedded software architecture design - message interaction

瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿

Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"

Getting started with rce

Assembly language - Beginner's introduction
随机推荐
Elk enterprise log analysis system
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Liar report query collection network PHP source code
Solve the problem of invalid uni app configuration page and tabbar
Rk3566 add LED
Comparison of several distributed databases
Recommendation number | what are interesting people looking at?
Laravel framework operation error: no application encryption key has been specified
LeetCode_67(二进制求和)
Liste des liens (simple)
Detailed explanation of SSH password free login
jasypt配置文件加密|快速入门|实战
ZABBIX monitoring
Solution to the prompt of could not close zip file during phpword use
Pancake Bulldog robot V2 (code optimized)
:: ffff:192.168.31.101 what address is it?
Require, require in PHP_ once、include、include_ Detailed explanation of the efficiency of repeated introduction of once class library
ETCD数据库源码分析——集群间网络层客户端peerRt
[js] basic syntax - for loop
神经网络物联网未来现状和趋势及看法