首页 > 其他分享 >jS 数据类型检测

jS 数据类型检测

时间:2023-11-09 11:02:20浏览次数:24  
标签:检测 Object 数据类型 jS let typeof constructor true

基本数据类型

Undefined、Null、Boolean、Number、String、Symbol(ES6 新增)和 BigInt(ES10 新增);

typeof

typeof str 通常检测数据的基本类型, 但对引用类型数据判断的话,除function会被识别出来之外,像null、{}、数组都输出为 object。

typeof null // 'object'
typeof undefined // 'undefined'
typeof 5 // 'number'
typeof '6' // 'string'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof {} // 'object'
typeof [] // 'object'

instanceof

obj instanceof Object 用于检测构造函数的原型属性是否出现在示例对象的原型链上,可以检测某个对象是否属于某个构造函数

// 定义构建函数
let Person = function() {}
let man = new Person()
man instanceof Person // true
let person = new String('xxx')
person instanceof String // true
let str = 'string'
str instanceof String // false

//关于 instanceof 的实现原理,如下:
//顺着原型链去找,直到找到相同的原型对象,返回true,否则为false
function myInstanceof(left, right) {
    // 这里先用typeof来判断基础数据类型,如果是,直接返回false
    if(typeof left !== 'object' || left === null) return false;
    // getProtypeOf是Object对象自带的API,能够拿到参数的原型对象
    let proto = Object.getPrototypeOf(left);
    while(true) {                  
        if(proto === null) return false;
        if(proto === right.prototype) return true;//找到相同原型对象,返回true
        proto = Object.getPrototypeof(proto);
    }
}

constructor

constructor 属性返回对象的构造函数。

let a = 1
let b = "123"
let c = true
console.log(a.constructor === Number)//true
console.log(b.constructor === String)//true
console.log(c.constructor === Boolean)//true

// 不会顺着原型链找
let arr = []
console.log(arr.constructor === Array)
console.log(arr.constructor === Object)

//缺点是可以随意更改constructor,例如
let arr = []
Array.prototype.constructor = 'a'  //更改constructor
console.log(arr.constructor === Array)//false

Object.prototype.toString.call

这个方法是最准确的检测数据类型的方法,这里的 toString 并不是转为字符串,而是返回当前实例所属的类信息的字符串。因为每个对象都有 toString 方法。

然后通过 call 将 this 指向要检测的 Object。

let a = 123
console.log(Object.prototype.toString.call(a))//[object Number]

总结

  • typeof只能检测除null外的基本数据类型,对于数组、对象、正则等都返回为Object
  • instanceof不能检测基本数据类型,检测引用类型时会顺着原型链往上找,只要原型链上有的,都返回true,同时,可以随意更改原型链的指向,导致检测结果不准确
  • constructor可以检测基本数据类型,也能分辨出数组和对象,但是我们可以随意更改constructor的值,导致检测结果不准确
  • Object.prototype.toString.call(value)可以根据返回的信息准确的检测出被测数据的类型



标签:检测,Object,数据类型,jS,let,typeof,constructor,true
From: https://blog.51cto.com/u_14914383/8274016

相关文章

  • JS基础语法
    JavaScipt运行在浏览器的编程语言书写位置内部<body><script>alert('你好')</script></body>外部<body><scriptsrc="my.js"></script></body>创建js文件,与img标签相似行内输入输出语法输出//ale......
  • Java Fastjson反序列化漏洞研究
    一、Fastjson简介Fastjson是阿里巴巴的一个开源项目,在GitHub上开源,使用Apache2.0协议。它是一个支持JavaObject和JSON字符串互相转换的Java库。Fastjson最大的特点在于它的快速,它超越了JackJson、Gson等库。据官方发布的说明,Fastjson从2011年fastjson发布1.1.x版本之后,其性能......
  • pytest + yaml 框架 -58.运行报告总结summary.json
    前言用例运行结束后,在本地生成summary.json文件,总结运行结果。v1.5.1版本更新内容:1.解决参数化,中文在控制台输出问题2.保存用例结果summary.json保存用例结果summary.json命令行执行用例pytest运行结束,在当前目录生成summary.json文件,内容如下{"base_url":"http......
  • Redis系列之常见数据类型应用场景
    目录String简单介绍常见命令应用场景Hash简单介绍常见命令应用场景List简单介绍常见命令应用场景Set简单介绍常见命令应用场景SortedSet(Zset)简单介绍常见命令应用场景Bitmap简单介绍常见命令应用场景附录Redis支持多种数据类型,比如String、hash、list、Set、SortedSet、Stream......
  • python json.loads()字符串转json
    python json.loads()字符串转jsonimportjsonimportrequestsres='''{"code":200,"message":"success","duration":147,"result":{"angle":0,"height":368,"tables":[],&qu......
  • 如何解决LocalDateTime传值JSON格式化问题
    LocalDateTime传值JSON格式化问题推荐方法其它方法LocalDateTime的json格式化问题解决方式一解决方式二LocalDateTime传值JSON格式化问题LocalDateTime是JDK8中提供的新功能,极大的优化了原生日期时间类的使用。但是第一次使用该类可能会在传值过程中出现格式化的小问题(如:JSON无法解......
  • PowerShell 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth o
    问题描述创建PowerShellAzureDurableFunction,执行大量的PowerShell脚本操作AzureResource,遇见了一个非常非常奇怪的问题:Function'Hello1(Activity)'failedwithanerror.Reason:Newtonsoft.Json.JsonReaderException:Thereader'sMaxDepthof64hasbeenexceeded.Pa......
  • 【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReade
    问题描述创建PowerShellAzureDurableFunction,执行大量的PowerShell脚本操作AzureResource,遇见了一个非常非常奇怪的问题:Function'Hello1(Activity)'failedwithanerror.Reason:Newtonsoft.Json.JsonReaderException:Thereader'sMaxDepthof64hasbeenexceeded.......
  • 【开源】基于Vue.js的学生日常行为评分管理系统的设计和实现
    一、摘要1.1项目介绍基于Vue+SpringBoot+MySQL的学生日常行为评分管理系统,包含了评分项目模块、评分数据模块,还包含系统自带的用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示等基础模块,学生日常行为评分管理系统基于角色的访问控制,给学生、......
  • js前端编码规范
    1、编码风格1.1强制两行缩紧1.2强制统一以分号结束语句1.3强制逗号分隔多行结构,始终加上最后一个逗号1.4推荐使用大括号包裹代码块1.4.3强制不适用空代码块1.5强制空格风格1.6推荐文件末保留一行空行;在块末和新语句间插......