当前位置:网站首页>JS判断对象类型方法_typeof怎么用_instanceof怎么用_constructor怎么用_Object.prototype.toString()怎么用
JS判断对象类型方法_typeof怎么用_instanceof怎么用_constructor怎么用_Object.prototype.toString()怎么用
2020-11-09 12:58:00 【osc_jpycizb5】
JS判断对象类型方法
typeof
typeof 方法 右边要跟一个 数据 就能返回这个数据的数据类型 注意typeof 不是方法 不需要加()
返回的数据类型有 string(字符串) number(数字) object(对象)array(数组)boolean(布尔)null(空)undefined(未定义)
var x = []
var y = {}
var z =null
var a
console.log( typeof 'aaa') // string
console.log( typeof 123) // number
console.log( typeof true) // boolean
console.log( typeof a) // undefined
console.log( typeof b) // undefined 未声明也是 undefined
console.log( typeof x) // object 数组也是对象 但是用typeof 不能检测出是数组
console.log( typeof y) // object
console.log( typeof z) // object null是空的对象 用typeof检测也是对象
instanceof
instanceof 用来判断 instanceof 前的 数据 是否 为 instanceof 后的类型 返回一个布尔值 同样不用加() 但是instanceof只能用来检测对象
var x = []
var y = {}
var z =null
var a = 'aaa'
console.log(x instanceof Object) //true 数组也是对象
console.log(x instanceof Array) //true
console.log(y instanceof Object) //true
console.log(y instanceof Array) //false 但数组不一定是对象
console.log(z instanceof Object) //false 用instanceof检测空对象 也是检测不出来的
console.log(a instanceof String) //false instanceof只能用来判断对象 对字符串无效
constructor
constructor 是通过原型链进行查找的 可以用来查找对象和普通数据 相比于 instanceof 和 typeof 会更加精准
var x = []
var y = {}
var z = Date()
var a = 'aaa'
console.log(x.constructor==Object) //false 在这里 数组并不被识别成对象
console.log(x.constructor==Array) //true
console.log(y.constructor==Object) //true
console.log(a.constructor==String) //true 字符串可以正常被识别
console.log(z.constructor==Object) //false 时间对象也不被识别成对象
console.log(z.constructor==Date) // true
Object.prototype.toString()
Object.prototype.toString() 是Object原型的方法 相比于以上三个 更加精准
var x = []
var y = {}
var z = new Date()
var a = 'aaa'
console.log(Object.prototype.toString.call(x)) //[object Array]
console.log(Object.prototype.toString.call(y)) //[object Object]
console.log(Object.prototype.toString.call(z)) //[object Date]
console.log(Object.prototype.toString.call(a)) //[object String]
版权声明
本文为[osc_jpycizb5]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4286379/blog/4709426
边栏推荐
- From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
- 接口测试如何在post请求中传递文件
- 零基础IM开发入门(四):什么是IM系统的消息时序一致性?
- Several methods of initializing singleton variable in go language
- 关于无相互作用极化率的计算
- 彩虹排序 | 荷兰旗问题
- Three practical skills of Medical Project Management
- EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
- An attempt to read or write to protected memory occurred using the CopyMemory API. This usually indicates that other memory is corrupted.
- 【分布式】分布式锁都有哪些实现方案?
猜你喜欢

移动安全加固助力 App 实现全面、有效的安全防护

Oh, my God! Printing log only knows log4j?

android studio AIDL的使用

From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice

Android studio AVD

What really drags you down is sunk costs

为wget命令设置代理

实现商品CRUD操作

接口测试如何在post请求中传递文件

Wechat circle
随机推荐
Is SEO right or wrong?
技美那么贵,不如找顾问 | AALab企业顾问业务
AndroidStudio导入定制化的framework classess.jar AS 4.0.1版本亲测有效
Reread reconstruction
移动安全加固助力 App 实现全面、有效的安全防护
通配符SSL证书应该去哪里注册申请
服务应用 ClockService安卓实现闹钟
7-10倍写入性能提升:剖析WiredTiger数据页无锁及压缩黑科技
Complete set of linked list operations of data structure and algorithm series (3) (go)
As a user, you can't get rid of the portrait!
Kubernetes business log collection and monitoring
Viewing PDB files from the angle of assembly
零基础IM开发入门(四):什么是IM系统的消息时序一致性?
Adobe Experience Design /Xd 2020软件安装包(附安装教程)
块级元素和行内元素
SQL statement to achieve the number of daffodils
Understanding runloop in OC
Introduction to zero based im development (4): what is message timing consistency in IM systems?
Explain Python input() function: get user input string
微信视频号播主排行榜2020年10月