javascript怎么判断是否为对象
时间:2021-04-07 来源:互联网
今天PHP爱好者给大家带来判断方法:1、使用toString()来判断;2、使用“obj.constructor === Object”来判断;3、使用“ypeof obj === Object”来判断;4、利用instanceof关键字来判断。希望对大家有所帮助。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
1、toString() 第一选择
let obj = {}
Object.prototype.toString.call(obj) === '[Object Object]'
2、constructor
let obj = {}
obj.constructor === Object
3、instanceof
注意:使用instanceof对数组进行判断也是对象
let obj = {}
obj instanceof Object //true
let arr = []
arr instanceof Object //true
4、typeof
let obj = {}
typeof obj === Object
// 根据typeof判断对象也不太准确
表达式 返回值
typeof undefined 'undefined'
typeof null 'object'
typeof true 'boolean'
typeof 123 'number'
typeof "abc" 'string'
typeof function() {} 'function'
typeof {} 'object'
typeof [] 'object'
以上就是javascript怎么判断是否为对象的详细内容,更多请关注php爱好者其它相关文章!
-
比特币买豪宅》佳士得开设加密货币房地产部门,隐私与效率吸引富豪买家 2025-07-27
-
香港稳定币条例 8/1 生效:牌照、监管、银行託管…重点速看 2025-07-27
-
KGNCloud 重新定义2025年数字财富——更智能、更快速、更安全的加密挖矿未来 2025-07-27
-
在另类币季节的狂热中,XRP惊现1.55亿美元巨额资金流入 2025-07-27
-
-
柴犬币(SHIB)在异常大额交易中暴涨25,587% 2025-07-27