当前位置:网站首页>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 .
边栏推荐
- Fault analysis | analysis of an example of MySQL running out of host memory
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
- [js] basic syntax - for loop
- 链表(简单)
- matlab学习2022.7.4
- 锚点导航小demo
- LeetCode_2(两数相加)
- Xampp configuring multiple items
- OSI and tcp/ip protocol cluster
- C - Divisors of the Divisors of An Integer Gym - 102040C
猜你喜欢

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

LeetCode_2(两数相加)

Network security - Novice introduction

Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑

为什么我认识的机械工程师都抱怨工资低?
![UE源码阅读[1]---由问题入手UE中的延迟渲染](/img/fa/f33242b01e4da973fa36c2c6f23db6.png)
UE源码阅读[1]---由问题入手UE中的延迟渲染

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

神经网络物联网未来发展趋势怎么样

金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功

Zibll theme external chain redirection go page beautification tutorial
随机推荐
Sqllab 1-6 exercise
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
神经网络物联网未来现状和趋势及看法
Elfk deployment
Convolutional Neural Networks简述
Google EventBus 使用详解
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
03_Solr之dataimport
2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
Blue Bridge Cup study 2022.7.5 (morning)
Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Embedded software architecture design - message interaction
C - Divisors of the Divisors of An Integer Gym - 102040C
matlab学习2022.7.4
PHP generate Poster
JS takes key and value from an array object to form a new object
upload (1-6)
Self built shooting range 2022
:: ffff:192.168.31.101 what address is it?