当前位置:网站首页>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 .
边栏推荐
- 神经网络物联网未来现状和趋势及看法
- C - Divisors of the Divisors of An Integer Gym - 102040C
- 神经网络物联网未来发展趋势怎么样
- poi设置列的数据格式(有效)
- What is information security? What is included? What is the difference with network security?
- Attack and defense world web WP
- 我为什么支持 BAT 拆掉「AI 研究院」
- js 从一个数组对象中取key 和value组成一个新的对象
- 清大科越冲刺科创板:年营收2亿 拟募资7.5亿
- 广发期货排名多少?网上办理广发期货开户安全可靠吗?
猜你喜欢
TiCDC 6.0原理之Sorter演进
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
What is the future development trend of neural network Internet of things
荐号 | 有趣的人都在看什么?
研究生可以不用学英语?只要考研英语或六级分数高!
基于 TiDB 场景式技术架构过程 - 理论篇
如何深入理解“有限状态机”的设计思想?
Why do I support bat to dismantle "AI research institute"
Those things I didn't know until I took the postgraduate entrance examination
Elfk deployment
随机推荐
POI set the data format of the column (valid)
2022司钻(钻井)考试题库及模拟考试
判断变量是否为数组
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
IP packet header analysis and static routing
神经网络物联网未来发展趋势怎么样
Source code analysis of etcd database -- peer RT of inter cluster network layer client
How to divide a large 'tar' archive file into multiple files of a specific size
链表(简单)
锚点导航小demo
常见问题之PHP——Fatal error: Allowed memory size of 314572800 bytes exhausted...
Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet
LeetCode_67(二进制求和)
Selenium crawls Baidu pictures
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
Requests + BS4 crawl Douban top250 movie information
Linux下mysql数据库安装教程
Login interface code
Detailed explanation of SSH password free login
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder