当前位置:网站首页>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 .
边栏推荐
- PHP generate Poster
- What category does the Internet of things application technology major belong to
- Jetpack Compose入门到精通
- [machine learning notes] how to solve over fitting and under fitting
- Some ideas about Apache mesos
- Jetpack compose introduction to mastery
- When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
- 登录界面代码
- Rk3566 add LED
- :: ffff:192.168.31.101 what address is it?
猜你喜欢

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

tidb-dm报警DM_sync_process_exists_with_error排查

基于 TiDB 场景式技术架构过程 - 理论篇

TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析

研究生可以不用学英语?只要考研英语或六级分数高!

Attack and defense world crypto WP

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

Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success

治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东

Detailed explanation of IP address and preparation of DOS basic commands and batch processing
随机推荐
Laravel generate entity
PHP character capture notes 2020-09-14
Implementation process of WSDL and soap calls under PHP5
Recommendation number | what are interesting people looking at?
关于memset赋值的探讨
The forked VM terminated without saying properly goodbye
根据CronSequenceGenerator计算cron表达式的时间
享你所想。智创未来
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
物联网应用技术专业是属于什么类
Primary code audit [no dolls (modification)] assessment
Rk3566 add LED
Hide Chinese name
What is the future development trend of neural network Internet of things
牛客网:拦截导弹
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
Leetcode array question brushing notes
链表(简单)
Attack and defense world web WP
[buuctf.reverse] 152-154