首页 > 其他分享 >每日一题:如何判断是否是数组,一个既简单又复杂的问题。(不要再用Object.prototype.toString.call、instance of判断了!!!)

每日一题:如何判断是否是数组,一个既简单又复杂的问题。(不要再用Object.prototype.toString.call、instance of判断了!!!)

时间:2023-09-20 13:57:09浏览次数:37  
标签:arr const Object instance toString obj console Array

1、不要使用Object.prototype.toString.call()

正常情况下:

const arr = [1,2,3,4,5]
const obj = {}
console.log(Object.prototype.toString.call(arr))//[Object,Array]
console.log(Object.prototype.toString.call(obj))//[Object,Object]

过去我们能够通过判断Object.prototype.toString.call(arr)第二个单词是否为Array来判断是否为数组,
但是时代变了,现在不行了。因为现在可以使用一个知名符号 toStringTag 改变生成的数组
例如:

const arr = [1,2,3,4,5]
const obj = {
    [Symbol.toStringTag]:'Array',
}
console.log(Object.prototype.toString.call(arr))//[Object,Array]
console.log(Object.prototype.toString.call(obj))//[Object,Array]

看到了吗?对象用这个方法,也可以把第二个单子变成Array。

虽然概率很小,但是不得不防。还是那句话你可以不用,但是你不能知道!!!

2、不要使用instance of

原理是通过判断一个对象上有没有Array的原型
正常情况下:

const arr = [1,2,3,4]
const obj = {}
console.log(arr instanceof Array) //true
console.log(obj instanceof Array) //false

说完正常情况下,就说一说不正常的情况下

  1. 通过Object.setPrototypeOf 将对象的原型改变为数组的原型
const arr = [1,2,3,4]
const obj = {}
Object.setPrototypeOf(obj,Array.prototype)
console.log(arr instanceof Array) //true
console.log(obj instanceof Array) //true

将对象也判断成数组了 ,哒咩

  1. 在页面环境中有iframe
    由于iframe会生成一套独立的document和独立的window,但是Array是window一个全局属性,这就造成了一个非常有意思的现象
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<iframe src="" frameborder="0"></iframe>
</body>
<script>
    const Array1 = window.Array
    const iframe = document.querySelector('iframe')
    const Array2 = iframe.contentWindow.Array
    const arr = new Array2()
    console.log(arr instanceof Array) //false
</script>
</html>

虽然我觉得出现这种情况的概率很小,但是还是那句话你可以不用,但是你不能知道!!!

3、唯一解:Array.isArray

const arr = [1,2,3,4,5]
//上述两种情况均可用Array.isArray判断
Array.isArray(arr) //true

4、免费的chatGPT

https://www.hangyejingling.cn/

标签:arr,const,Object,instance,toString,obj,console,Array
From: https://www.cnblogs.com/never404/p/17717137.html

相关文章

  • ToString()格式和用法大全,C#实现保留两位小数的方法
    C,货币,2.5.ToString("C"),¥2.50。D,十进制数,25.ToString("D5"),00025。E,科学型,25000.ToString("E"),2.500000E+005。F,固定点,25.ToString("F2"),25.00。G,常规,2.5.ToString("G"),2.5。N,数字,2500000.ToString("N"),2,500,000.0......
  • unity Editor的target和serializedObject
    转自:Editor.target与Editor.serializedObject|那些遇到过的问题(1r1g.com)首先,有一个CanEditMultipleObjects您目前没有使用的选项。文档中的引用:如果使用这种方法,用户可以在层次结构窗口中选择多个资产并一次更改所有资产的值。作为一个基本示例,GameObjects在场景中选择......
  • 解决git 问题:loose object is corrupt
    问题:error:objectfile.git/objects/e1/refisemptyerror:objectfile.git/objects/e1/refisemptyfatal:looseobjecte1ref(storedin.git/objects/e1/ref)iscorrupt解决方式:find.git/objects/-size0-execrm-f{}\;gitfetchorigin 参考:ht......
  • Mongo Template 禁用 _id 字段自动转 ObjectId
    目前情况下,只能在实体中不使用@Id而使用 @Field("_id"),让自动转换那边的isIdField判断为false方式~~~@Field("_id")privateStringid;~~~使用@Id注解;或者使用MongoTemplate指定集合名称的系列方法,都会走自动转ObjectId......
  • Ceph Object Gateway
    本次演示环境配置如下:hostnameIProlesnode01.srv.world192.168.10.101ObjectStorage;MonitorDaemon;ManagerDaemonnode02.srv.world192.168.10.102ObjectStoragenode03.srv.world192.168.10.103ObjectStoragedlp.srv.world192.168.10.142clientwww.srv.world192.168.10.140RAD......
  • How to fix Node.js fs.readFileSync toString Error All In One
    HowtofixNode.jsfs.readFileSynctoStringErrorAllInOneSyntaxError:UnexpectedendofJSONinput❌errorfs.writeFile&fs.readFileSync匹配错误asyncappendFile(group){console.log(`append`)constfile=path.join(__dirname+`/vide......
  • AttributeError: 'int' object has no attribute 'items' 混合数据存储
    data={'2023:09:01':{'867726032728067':68},'2023:09:02':{'867726032728067':68},'2023:09:03':0,'2023:09:04':{'866384064965578':48,'867266067918648':46......
  • Animation动画——ObjectAnimator基本使用
    ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能。这使得为任何对象设置动画更加的容易。你不再需要实现ValueAnimator.AnimatorUpdateListener接口,因为ObjectAnimator动画自己会自动更新相应的属性值。ObjectAn......
  • 【JS】实现 instanceOf
    https://github.com/zjy4fun/notes/tree/main/demos/js-instanceof原型就是一个对象,instanceof就是检查构造函数的原型是否在对象的原型链上 functionmyInstanceOf(obj,constructorFn){constprototype=constructorFn.prototypewhile(obj!==null){......
  • CentOS7.5报java: error while loading shared libraries: libjli.so: cannot open sh
    1.问题描述:CentOS版本:CentOS-7.5-x86_64-DVD-1804jdk版本:jdk-8u161-linux-x64.tar配置jdk时,执行java报错java:errorwhileloadingsharedlibraries:libjli.so:cannotopensharedobjectfile:Nosuchfileordirectory 2.解决方法:发现是CentOS7.5不支持jdk-8u161......