当前位置:网站首页>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 .
边栏推荐
- [cloud resources] what software is good for cloud resource security management? Why?
- 别不服气。手机功能升级就是强
- Current situation, trend and view of neural network Internet of things in the future
- Routing in laravel framework
- tidb-dm报警DM_sync_process_exists_with_error排查
- 基于微信小程序的订餐系统
- NFT value and white paper acquisition
- PHP basic syntax
- js 从一个数组对象中取key 和value组成一个新的对象
- 让秒杀狂欢更从容:大促背后的数据库(下篇)
猜你喜欢
随机推荐
展现强大。这样手机就不会难前进
Rk3566 add LED
:: ffff:192.168.31.101 what address is it?
2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
About the problem and solution of 403 error in wampserver
Zhubo Huangyu: it's really bad not to understand these gold frying skills
[buuctf.reverse] 152-154
TDengine 社区问题双周精选 | 第三期
基于微信小程序的订餐系统
When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
laravel-dompdf导出pdf,中文乱码问题解决
uplad_ Labs first three levels
[machine learning notes] how to solve over fitting and under fitting
OSI and tcp/ip protocol cluster
Brief introduction to revolutionary neural networks
Implementation process of WSDL and soap calls under PHP5
如何深入理解“有限状态机”的设计思想?
Assembly language - Beginner's introduction
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%

![Primary code audit [no dolls (modification)] assessment](/img/b8/82c32e95d1b72f75823ca91c97138e.jpg)

![[js] basic syntax - for loop](/img/7f/6ddc47c062caa7d39538f88e12b1a0.jpg)


![UE源码阅读[1]---由问题入手UE中的延迟渲染](/img/fa/f33242b01e4da973fa36c2c6f23db6.png)


