当前位置:网站首页>判断一个变量是数组还是对象?
判断一个变量是数组还是对象?
2022-06-27 17:41:00 【程序员Grand.】
一、变量声明
var arr = []
var obj = {
}
二、判断方式
(1)Array.isArray()
Array.isArray()是ES6提供判断数组的方法,但它有兼容性问题。
Array.isArray(arr) // true
Array.isArray(obj) // false
(2)instanceof 和 constructor
instanceof返回一个布尔值,表示对象是否为某个构造函数的实例。instanceof的左边是实例对象,右边是构造函数,它会检查右边构造函数是否在左边实例对象的原型链上。
原型对象prototype有一个constructor属性,默认指向原型对象prototype所在的构造函数。
console.log(arr instanceof Array); // true
console.log(arr.constructor === Array); // true
但是,instanceof和constructor有弊端。举例如下:
var iframe = document.creatElement('iframe')
document.body.append(iframe)
var xArray = window.frames[window.frames.length - 1].Array
var arr = new xArray(1,2,3)
console.log(a instanceof Array); // false
console.log(a.constructor === Array); // false
(3)Object.prototype.toString.call()
console.log(Object.prototype.toString.call(arr) === '[object Array]') // true
为什么不直接使用obj.toString() === '[object Array]'?
var obj = {
toString:function(){
return '1'
}
}
若对象里重写了toString方法的话,obj.toString() === '[object Array]'为false。
边栏推荐
- NVIDIA Clara-AGX-Developer-Kit installation
- RANSAC的代码和原理
- Market status and development prospect forecast of global active quality control air sampler industry in 2022
- 国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
- A simple calculation method of vanishing point
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- 作用域-Number和String的常用Api(方法)
- im即时通讯开发之双进程守护保活实践
- labelimg使用指南
- Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
猜你喜欢

实施MES管理系统前,要对哪些问题进行评估

Redis 原理 - String

2022年第一季度消费金融APP用户洞察——总数达4479万人

Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded

过关斩将,擒“指针”(下)

New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million

基于STM32F103ZET6库函数外部中断实验

Rxjs mergeMap 的使用场合

Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders

如何实现IM即时通讯“消息”列表卡顿优化
随机推荐
Market status and development prospect forecast of global tetramethylammonium hydroxide developer industry in 2022
Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
Cdga | what is the core of digital transformation in the transportation industry?
数仓的字符截取三胞胎:substrb、substr、substring
Bit. Store: long bear market, stable stacking products may become the main theme
华大单片机KEIL报错_WEAK的解决方案
Don't worry. This is the truth about wages in all industries in China
Keras深度学习实战(12)——面部特征点检测
Running lantern experiment based on stm32f103zet6 library function
深度学习和神经网络的介绍
Summary of domestic database certification test guide (updated on June 16, 2022)
国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
实战回忆录:从Webshell开始突破边界
破解仓储难题?WMS仓储管理系统解决方案
Keras deep learning practice (12) -- facial feature point detection
Character interception triplets of data warehouse: substrb, substr, substring
Tupu digital twin intelligent energy integrated management and control platform
惊呆!原来 markdown 的画图功能如此强大!
实施MES管理系统前,要对哪些问题进行评估
xctf攻防世界 MISC薪手进阶区