当前位置:网站首页>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 .
边栏推荐
- 2022 construction welder (special type of construction work) special operation certificate examination question bank and online simulation examination
- 2022建筑焊工(建筑特殊工种)特种作业证考试题库及在线模拟考试
- [buuctf.reverse] 152-154
- Sqllab 1-6 exercise
- The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
- WebRTC的学习(二)
- Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet
- Laravel generate entity
- 神经网络物联网未来现状和趋势及看法
- Laravel - view (new and output views)
猜你喜欢
牛客网:拦截导弹
Jetpack compose introduction to mastery
IP packet header analysis and static routing
Attack and defense world web WP
Sqllab 1-6 exercise
Brief introduction to revolutionary neural networks
Getting started with rce
-Web direction attack and defense world
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
Elfk deployment
随机推荐
Attack and defense world crypto WP
Detailed explanation of SSH password free login
2022司钻(钻井)考试题库及模拟考试
Implementation process of WSDL and soap calls under PHP5
How to deal with the Yellow Icon during the installation of wampserver
Jetpack compose introduction to mastery
Leetcode array question brushing notes
uplad_ Labs first three levels
[buuctf.reverse] 152-154
PHP basic syntax
2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
LeetCode_67(二进制求和)
Laravel generate entity
Selenium crawls Baidu pictures
Recommendation number | what are interesting people looking at?
深拷贝真难
【云资源】云资源安全管理用什么软件好?为什么?
:: ffff:192.168.31.101 what address is it?
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
LeetCode_69(x 的平方根 )