当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- JVM learning (6) - memory model and thread
- Explain Python input() function: get user input string
- The choice of domain name of foreign trade self built website
- A simple way to realize terminal text paste board
- 阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
- 嗯,查询滑动窗口最大值的这4种方法不错...
- Mobile security reinforcement helps app achieve comprehensive and effective security protection
- 医疗项目管理的三种实用技巧
- Large scale project Objective-C - nsurlsession access SMS verification code application example sharing
- 除了解析域名,DNS还能干吗?
猜你喜欢

Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive

What really drags you down is sunk costs

天啦撸!打印日志竟然只晓得 Log4j?

手写Koa.js源码

解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)

03. Priority link model

零基础IM开发入门(四):什么是IM系统的消息时序一致性?

Idea solves garbled Chinese output of YML configuration file

Wechat circle

On the calculation of non interaction polarizability
随机推荐
SEO见风使舵,是对还是错?
10款必装软件,让Windows使用效率飞起!
Android studio AVD
Android rights
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
Rainbow sorting | Dutch flag problem
如何用函数框架快速开发大型 Web 应用 | 实战
接口测试如何在post请求中传递文件
Is SEO right or wrong?
Reduce of Flink
inet_pton()和inet_ntop()函数详解
“开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
Interview summary on November 7, 2020 (interview 12K)
彩虹排序 | 荷兰旗问题
Efficient estimation of word representations in vector space
Android 集成支付的四部曲
Solve the problem of idea shortcut key Alt + insert invalid
Flink 系例 之 Reduce
Learn with me. NETCORE EF core practical introduction, a look will
IDEA解决yml配置文件中文输出乱码问题