当前位置:网站首页>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
边栏推荐
- 彩虹排序 | 荷兰旗问题
- Android Studio Avd「真·小白食用方法」
- 技美那么贵,不如找顾问 | AALab企业顾问业务
- Configure switch trunk interface traffic local priority forwarding (cluster / stack)
- 20201107第16课,使用Apache服务部署静态网站;使用Vsftpd服务传输文件
- Complete set of linked list operations of data structure and algorithm series (3) (go)
- 用一种简单的方式实现终端文字粘贴板
- 微信视频号播主排行榜2020年10月
- Show profile analysis of SQL statement performance overhead
- For and for... In, for each and map and for of
猜你喜欢
随机推荐
FGC online service troubleshooting, this is enough!
Reread reconstruction
Gather in Beijing! Openi / O 2020 Qizhi Developer Conference enters countdown
Handwriting Koa.js Source code
JVM learning (4) - garbage collector and memory allocation
A simple way to realize terminal text paste board
New features of Fedora 33 workstation
The middle stage of vivo Monkey King activity
Fedora 33 Workstation 的新功能
真正拖垮你的,是沉没成本
Is SEO right or wrong?
为wget命令设置代理
Android Studio Avd「真·小白食用方法」
What really drags you down is sunk costs
Navigation component of Android architecture (2)
Windows must be installed with efficiency software!
Reduce of Flink
In the future, China Telecom will make cloud computing service the main business of China Telecom
How to use function framework to develop large web application
为什么我强烈建议要定制开发小程序,这些好处你需要了解






