当前位置:网站首页>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 .
边栏推荐
- SSH免密码登录详解
- OSI and tcp/ip protocol cluster
- Hide Chinese name
- Xampp configuring multiple items
- 别不服气。手机功能升级就是强
- What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?
- -Web direction attack and defense world
- 蓝桥杯学习2022.7.5(上午)
- LeetCode_69(x 的平方根 )
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
猜你喜欢

【云资源】云资源安全管理用什么软件好?为什么?

-Web direction attack and defense world

Rk3566 add LED

Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!

瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿

LeetCode_2(两数相加)

Xampp configuring multiple items

Comparison of several distributed databases

基于微信小程序的订餐系统
Jetpack compose introduction to mastery
随机推荐
OSI and tcp/ip protocol cluster
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
Selenium crawls Baidu pictures
Sqllab 1-6 exercise
广发期货排名多少?网上办理广发期货开户安全可靠吗?
01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
牛客网:拦截导弹
Elk enterprise log analysis system
链表(简单)
2022建筑焊工(建筑特殊工种)特种作业证考试题库及在线模拟考试
NFT value and white paper acquisition
锚点导航小demo
LeetCode_3(无重复字符的最长子串)
Jetpack Compose入门到精通
Anchor navigation demo
Liste des liens (simple)
[cloud resources] what software is good for cloud resource security management? Why?
基于微信小程序的订餐系统
LeetCode_2(两数相加)
In addition to the root directory, other routes of laravel + xampp are 404 solutions