码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 判断是否为对象

时间:2020-07-04 19:14:18      阅读:549      评论:0      收藏:0      [点我收藏+]

标签:detail   undefined   string   prototype   zha   boolean   表达   cal   net   

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‘

原文:https://blog.csdn.net/zhangjing0320/article/details/81230170

 

JavaScript 判断是否为对象

标签:detail   undefined   string   prototype   zha   boolean   表达   cal   net   

原文地址:https://www.cnblogs.com/zhangyezi/p/13236010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!