当前位置:网站首页>Judge whether the variable is an array
Judge whether the variable is an array
2022-07-05 14:05:00 【Front end watermelon brother】
Hello everyone , I'm brother watermelon , Today I will take you to learn in JS How to determine whether an object is an array .
Array.isArray
The best way to write it is to use Array.isArray(val).
Because this method can correctly judge iframe The variable passed .
instanceof
val instanceof Array
For a normal array , Its prototype chain is like this :
Array instance -> Array.protype -> Object.prototype -> null
If a variable is an array , Then there must be on its prototype chain Array.prototype.
instanceof In fact, the principle of l-value follows its prototype chain , And right value prototype Attribute comparison , See if it's equal . Find the equivalent and return true, Otherwise return to false.
but instanceof Unable to deal with iframe The variable of , because iframes Of Array Not the current page Array, They point to their respective memory space .
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const ArrayInFrame = window.frames[0].Array; // iframe Constructors in
const arr = new ArrayInFrame();
arr instanceof Array // false
Array.isArray(arr); // true
As long as you can judge Whether the object is on the prototype chain All methods can be used to determine whether it is an array , So you can also use :
// isPrototypeOf Method
// Array.prototype Whether in val On the prototype chain
Array.prototype.isPrototypeOf(val);
// getPrototypeOf Method
// val Whether the last prototype of is Array.prototype
Object.getPrototypeOf(val) === Array.prototype
Object.prototype.toString.call
Object.prototype.toString.call(val) === '[object Array]';
Objects can be accessed through toString Method to generate a string , For the object , Usually back to [object Xxx] A string in the form of , For arrays , Will return [object Array].
ending
Considering compatibility iframe The variable of , So it is recommended to use Array.isArray programme .
In addition, the method of finding the prototype chain object can also be used to determine the array , And by toString The method of getting the string .
I'm brother watermelon , Welcome to follow me , Master more front-end interview questions .
边栏推荐
- 基于 TiDB 场景式技术架构过程 - 理论篇
- 昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
- 最简单不用证书也可以多开功能的方式
- Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
- Zhubo Huangyu: these spot gold investment skills are not really bad
- Requests + BS4 crawl Douban top250 movie information
- Implementation process of WSDL and soap calls under PHP5
- 链表(简单)
- Zhubo Huangyu: it's really bad not to understand these gold frying skills
- SAS接口有什么优势特点
猜你喜欢

Attack and defense world crypto WP

Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan

神经网络物联网未来现状和趋势及看法

::ffff:192.168.31.101 是一个什么地址?

金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功

如何将 DevSecOps 引入企业?

Liar report query collection network PHP source code
![[cloud resources] what software is good for cloud resource security management? Why?](/img/c2/85d6b4a956afc99c2dc195a1ac3938.png)
[cloud resources] what software is good for cloud resource security management? Why?

为什么我认识的机械工程师都抱怨工资低?

明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
随机推荐
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Fault analysis | analysis of an example of MySQL running out of host memory
蓝桥杯学习2022.7.5(上午)
Controller in laravel framework
享你所想。智创未来
广发期货排名多少?网上办理广发期货开户安全可靠吗?
What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?
Rk3566 add LED
Ordering system based on wechat applet
Liar report query collection network PHP source code
根据CronSequenceGenerator计算cron表达式的时间
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
Ueditor + PHP enables Alibaba cloud OSS upload
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
laravel-dompdf导出pdf,中文乱码问题解决
Getting started with rce
PHP character capture notes 2020-09-14
Selenium crawls Baidu pictures
Kunlun Taike rushes to the scientific innovation board: the annual revenue is 130million, and it plans to raise 500million. CETC Taiji holds 40% of the shares
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder