当前位置:网站首页>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 .
边栏推荐
- 别不服气。手机功能升级就是强
- ::ffff:192.168.31.101 是一个什么地址?
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
- 2022 driller (drilling) examination question bank and simulation examination
- Attack and defense world web WP
- Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
- PHP basic syntax
- 基于 TiDB 场景式技术架构过程 - 理论篇
- Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
- 瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
猜你喜欢

Recommendation number | what are interesting people looking at?

TiCDC 6.0原理之Sorter演进

Scientific running robot pancakeswap clip robot latest detailed tutorial

How to deal with the Yellow Icon during the installation of wampserver
![[js] basic syntax - for loop](/img/7f/6ddc47c062caa7d39538f88e12b1a0.jpg)
[js] basic syntax - for loop

Rk3566 add LED

Convolutional Neural Networks简述

TDengine 社区问题双周精选 | 第三期

常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...

Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
随机推荐
2022 driller (drilling) examination question bank and simulation examination
Detailed explanation of SSH password free login
MySQL if else use case use
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
2022年机修钳工(高级)考试题模拟考试题库模拟考试平台操作
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
Zhubo Huangyu: these spot gold investment skills are not really bad
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
Some ideas about Apache mesos
如何深入理解“有限状态机”的设计思想?
mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
享你所想。智创未来
PostgreSQL Usage Summary (PIT)
TiCDC 6.0原理之Sorter演进
What is the future development trend of neural network Internet of things
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
PHP character capture notes 2020-09-14
Routing in laravel framework
Jetpack compose introduction to mastery
为什么我认识的机械工程师都抱怨工资低?