当前位置:网站首页>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 .
边栏推荐
- 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
- Kotlin collaboration uses coroutinecontext to implement the retry logic after a network request fails
- 如何深入理解“有限状态机”的设计思想?
- Linux下mysql数据库安装教程
- Requset + BS4 crawling shell listings
- 治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
- 2022司钻(钻井)考试题库及模拟考试
- About the problem and solution of 403 error in wampserver
- 判断变量是否为数组
- poi设置列的数据格式(有效)
猜你喜欢
laravel-dompdf导出pdf,中文乱码问题解决
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
Current situation, trend and view of neural network Internet of things in the future
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
Self built shooting range 2022
IP packet header analysis and static routing
金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
Rk3566 add LED
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
随机推荐
最简单不用证书也可以多开功能的方式
Sqllab 1-6 exercise
Current situation, trend and view of neural network Internet of things in the future
Kotlin collaboration uses coroutinecontext to implement the retry logic after a network request fails
PHP generate Poster
鸿蒙第四次培训
享你所想。智创未来
Requset + BS4 crawling shell listings
Deep copy is hard
展现强大。这样手机就不会难前进
What category does the Internet of things application technology major belong to
Source code analysis of etcd database -- peer RT of inter cluster network layer client
根据CronSequenceGenerator计算cron表达式的时间
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
Laravel - model (new model and use model)
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
tidb-dm报警DM_sync_process_exists_with_error排查
Detailed explanation of SSH password free login
Brief introduction to revolutionary neural networks
几款分布式数据库的对比