当前位置:网站首页>JS method of judging object type_ How to use typeof_ How to use instanceof_ How to use constructor_ Object.prototype.toString How to use ()
JS method of judging object type_ How to use typeof_ How to use instanceof_ How to use constructor_ Object.prototype.toString How to use ()
2020-11-09 12:58:00 【http://www.google.com/sea....tin-round+rock+texas】
JS How to judge the object type
typeof
typeof Method There's a... on the right data You can return the data type of this data Be careful typeof It's not the way No need to add ()
The returned data types are string( character string ) number( Numbers ) object( object )array( Array )boolean( Boolean )null( empty )undefined( 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 It's also undeclared undefined
console.log( typeof x) // object Arrays are also objects But with typeof Can't detect array
console.log( typeof y) // object
console.log( typeof z) // object null It's an empty object use typeof Detection is also the object
instanceof
instanceof Used to determine instanceof front Of data whether by instanceof after The type of Returns a Boolean value No need to add () however instanceof Can only be used to detect objects
var x = []
var y = {}
var z =null
var a = 'aaa'
console.log(x instanceof Object) //true Arrays are also objects
console.log(x instanceof Array) //true
console.log(y instanceof Object) //true
console.log(y instanceof Array) //false But arrays don't have to be objects
console.log(z instanceof Object) //false use instanceof Detecting empty objects It can't be detected
console.log(a instanceof String) //false instanceof It can only be used to judge objects Invalid for Strings
constructor
constructor It's through the prototype chain Can be used to find objects and common data Compared with instanceof and typeof It will be more precise
var x = []
var y = {}
var z = Date()
var a = 'aaa'
console.log(x.constructor==Object) //false ad locum Arrays are not recognized as objects
console.log(x.constructor==Array) //true
console.log(y.constructor==Object) //true
console.log(a.constructor==String) //true Strings can be recognized normally
console.log(z.constructor==Object) //false Time objects are not recognized as objects
console.log(z.constructor==Date) // true
Object.prototype.toString()
Object.prototype.toString() yes Object The archetypal approach Compared with the above three More accurate
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]
版权声明
本文为[http://www.google.com/sea....tin-round+rock+texas]所创,转载请带上原文链接,感谢
边栏推荐
- How to use function framework to develop large web application
- Implement crud operation
- 嗯,查询滑动窗口最大值的这4种方法不错....
- 走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
- Idea rest client, yes, I haven't opened postman yet
- A simple ability determines whether you will learn!
- Android架构之Navigation组件(二)
- 跟我一起学.NetCore之EF Core 实战入门,一看就会
- 使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
- Android NDK 开发实战 - 微信公众号二维码检测
猜你喜欢

利用 Python 一键下载网易云音乐 10W+ 乐库

SQL Chapter 2 Chapter 3

Solve the problem of idea shortcut key Alt + insert invalid

Three practical skills of Medical Project Management

Interface tests how to pass files in post requests

Open source ERP recruitment

JVM learning (4) - garbage collector and memory allocation

块级元素和行内元素

在嵌入式设备中实现webrtc的第三种方式③

接口测试如何在post请求中传递文件
随机推荐
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
Solve the problem of idea shortcut key Alt + insert invalid
Well, the four ways to query the maximum value of sliding window are good
Windows must be installed with efficiency software!
线上服务的FGC问题排查,看这篇就够了!
7-10倍写入性能提升:剖析WiredTiger数据页无锁及压缩黑科技
Use treeview tree menu bar (recursively call database to create menu automatically)
Using rem, the font size changes when the screen zooms
服务应用 ClockService安卓实现闹钟
解决IDEA快捷键 Alt+Insert 失效的问题
Oh, my God! Printing log only knows log4j?
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
多线程真的比单线程快?
Introduction to zero based im development (4): what is message timing consistency in IM systems?
Ali, Tencent, Baidu, Netease, meituan Android interview experience sharing, got Baidu, Tencent offer
In the future, China Telecom will make cloud computing service the main business of China Telecom
IDEA rest-client,会了它我还没打开过postman
JVM学习(六)-内存模型和线程
导师制Processing网课 双十一优惠进行中
TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融