当前位置:网站首页>实习打怪之路:JS中检测数据类型的方法
实习打怪之路:JS中检测数据类型的方法
2022-07-22 00:18:00 【The..Fuir】
一.JS中的数据类型:
1) 简单类型:String、Number、Boolean、Undefined、Null、Symbol
2)引用(复杂)类型:Object二、检测数据类型的方法:
1、typeof 检测一些基本的数据类型
语法:typeof 后面加不加括号都是可以用的
注意:正则、{}、[]、null输出结果为object//typeof 检测一些基本的数据类型 var a=Symbol(); console.log(typeof a); console.log(typeof /\d/);//object console.log(typeof {});//object console.log(typeof []);//object console.log(typeof (null));//object console.log(typeof 123);//number console.log(typeof true);//boolean console.log(typeof function () {});//function console.log(typeof (undefined));//undefined console.log(typeof NAN)//undefined console.log(typeof [])//object
2、A instanceof B检测当前实例是否隶属于某各类
双目运算符 a instanceof b ,判断a的构造器是否为b,返回值为布尔值
//双目运算符 a instanceof b ,判断a的构造器是否为b,返回值为布尔值 function b(){} let a = new b; console.log(a instanceof b);//true console.log(b instanceof Object);//true let arr = [1,2,3,4]; console.log(arr instanceof Array);//true
3、constructor构造函数
语法:实例.constructor
对象的原型链下(构造函数的原型下)有一个属性,叫constructor
缺点:**constructor并不可靠,容易被修改(只有参考价值)。即使被修改了,也不会影响代码的正常运行。正常开发的时候,constructor不小心被修改了,为了方便维护,和开发,可以手动更正constructor的指向。//constructor构造函数 function b() {} let a = new b; let c=new Array(); console.log(a.constructor.name);//b console.log(c.constructor)//[Function: Array] console.log(b.constructor);//Function(){} console.log(Function.constructor);//Function(){}
小知识
4、hasOwnporperty 检测当前属性是否为对象的私有属性
语法: obj.hasOwnporperty(“属性名(K值)”)
// hasOwnporperty检测当前属性是否为对象的私有属性 let obj = { name:"lxw" }; console.log(obj.hasOwnProperty('name'));//true console.log(obj.hasOwnProperty('lxw'));//false
5、is Array 判断是否为数组
// 5、is Array 判断是否为数组 console.log(Array.isArray([]));//true console.log(Array.isArray(new Array()));//true
6、valueOf
可以看到数据最本质内容(原始值)
//valueOf let a = "12345"; let b= new String('12345'); console.log(typeof a);//string console.log(typeof b);//object console.log(a == b);//true (typeof检测出来是对象居然和一个字符串相等,显然b并不是一个真的对象。) //此时看看 valueOf()的表现 console.log(b.valueOf());//12345 拿到b的原始值是 字符串的12345 console.log(typeof b.valueOf())//string 这才是 b的真正的数据类型
1.Math是个对象,不是类。类才有prototype(原型)。也就是说类都有原型:prototype,对象都有原型链:proto,函数既是类,也是对象,也是函数。
2.如何把对象转换成字符串?
对象下的toString方法是检测数据类型的,而不是用来转化成字符串的,这时可以用JSON的方法来转化let obj={name:'朱军林'} console.log(obj.toString())//=>"[object Object]" console.log(JSON.stringify({name:'朱军林'}))//"{"name":"朱军林"}"
7、Object.portotype.toString (最好的)
语法:Object.prototype.toString.call([value])
获取Object.portotype上的toString方法,让方法的this变为需要检测的数据类型值,并且让这个方法执行在Number、String、Boolean、Array、Function、RegExp…这些类的原型上都有一个toString方法:这个方法就是把本身的值转化为字符串
(123).toString()//'123' (true).toString()//'true' [12,23].toString()//'12.23'
在Object这个类的原型上也有一个方法toString,但是这个方法并不是把值转换成字符串,而是返回当前值得所属类详细信息,固定结构:’[object 所属的类]'
调取的正是Object.prototype.toString方法obj.toString()
首先执行Object.prototype.tostring方法,这个方法中的this就是我们操作的数据值obj
总结:Object.prototype.toString执行的时候返回当前方法中的this的所属类信息,也就是,我想知道谁的所属类信息,我们就把这个toString方法执行,并且让this变为我们检测的这个数据值,那么方法返回的结果就是当前检测这个值得所属类信息Object.prototype.toString.call(12)//[boject Number] Object.prototype.toString.call(true)//[boject Boolean] //"[object Number]" //"[object String]" //"[object Object]" //"[object Function]" //"[object Undefined]" //"[object Null]" //"[object RegExp]" //"[object Boolean]"
Object.prototype.toString.call()
这个方法是用来判断数据类型的,主要用到的知识点是原型,原型链
每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。 默认情况下,toString() 方法被每个 Object 对象继承。如果此方法在自定义对象中未被覆盖,toString() 返回 "[object type]", 其中 type 是对象的类型。
如果把Array里面的toString方法删除
再调用toString()
输出这个的原因,是因为,Array里面的toString被删除掉,此时Array的toString,是继承自Object里面的toString。 所以此时可以判断数据类型。所以判断数据类型,直接用对象上面的Object.prototype.toString来判断数据类型
一个数组,调用了toString。返回了字符串 ,这是正常预期,为什么呢,因为Array和Object里都有toString() ,根据原型链的就近原则,会先取Array.prototype,
此时我们如果删除Array里面的toString
先检测Array里 有没有toString ,
此时,会发现,明明放进去的是数组,怎么返回了对象类型,因为Object.prototype 本身是对象类型,所以返回了对象类型,要是想判断传入值的类型,需要把传入值的this,指向Object.prototype ,才能判断传入值的类型
总结
1.toString为Object的原型方法,而Array 、Function等类型作为Object的实例,都重写了toString方法。
2.不同的对象类型调用toString方法时,根据原型链的知识,
调用的是对应的重写之后的toString方法(Function类型返回内容为函数体的字符串,Array类型返回元素组成的字符串…),而不会去调用Object上原型toString方法(返回对象的具体类型),3.所以采用obj.toString()不能得到其对象类型,只能将obj转换为字符串类型;
因此,在想要得到对象的具体类型时,应该调用Object上原型toString方法。4.Object对象本身就有一个toString()方法,返回的是当前对象的字符串形式,原型上的toString()返回的才是我们真正需要的包含对象数据类型的字符串。.
5.为什么需要call?
由于Object.prototype.toString()本身允许被修改,像Array、Boolean、Number的toString就被重写过,所以需要调用Object.prototype.toString.call(arg)来判断arg的类型,call将arg的上下文指向Object,所以arg执行了Object的toString方法。至于call,就是改变对象的this指向,当一个对象想调用另一个对象的方法,可以通过call或者apply改变其this指向,将其this指向拥有此方法的对象,就可以调用该方法了
原文链接:https://blog.csdn.net/qq_38845858/article/details/124344500
边栏推荐
- 数据库风格的DataFrame Joins)具体是什么呢?
- JUC-同步
- 酷早报:7月21日Web3加密行业新闻大汇总
- Take you to brush (niuke.com) C language hundred questions (day 3)
- 通讯录(文件版本)
- RenderFlex children have non-zero flex but incoming height constraints are unbounded.
- 6.zabbix报警设置
- Read data nearby under the three data centers
- JUC processes and threads
- Notes and Thoughts on the red dust of the sky (I) all results are conditional possibilities
猜你喜欢
Android 面试题:说一下 PendingIntent 和 Intent 的区别
Looking for Fibonacci number
After reading this, I can't DVMA yet. Please eat melon
Tidb high concurrency write scenario best practices
Cyberspace mapping
C language hierarchical understanding (C language branches and circular statements)
Interview project preparation | how to show the project to the interviewer?
Pytorch(三)——FashionMNIST时装分类
Make a reliable delay queue with redis
Pytorch (IV) -- pytorch model definition
随机推荐
咸鱼自救指南--typescript关于接口那些事
kube-scheduler的调度上下文
Network metering - transport layer
MySQL调优(D)
用 Redis 做一个可靠的延迟队列
通讯录(文件版本)
JUC processes and threads
Talk about how programmers improve their writing ability
Explore the implementation of hash table through redis source code
Android interview question: tell me the difference between pendingintent and intent
《天幕红尘》笔记与思考(二)你知道别人的认识和你自己知道
看完这个,还不会DVMA,请你吃瓜
Super dry! Thoroughly understand the differences and connections between simplex, half duplex and full duplex
PyTorch(四)——PyTorch模型定义
I met a 38K from Tencent two days ago, which showed me the ceiling of the foundation. Today I give it to you~
Interview 3 (multiple processes call the same dynamic library)
In the software testing interview, the interviewer asks you some "difficult" questions. How will you answer them
Esp8266 analog input (ADC) detection problem
Advanced C language: data storage (floating point)
5.zabbix创建自定义key