【代码】JavaScript判断是否是对象类型

2021-07-22 09:14:42  阅读 2278 次 评论 1 条
var obj = {};

1、toString(推荐)

Object.prototype.toString.call(obj) === '[object Object]'

2、constructor

obj.constructor === Object

3、instanceof 需要注意的是由于数组也是对象,因此用 arr instanceof Object 也为true。

obj instanceof Object

4、typeof

typeof obj === Object
表达式返回值
typeof undefined'undefined'
typeof null'object'
typeof true'boolean'
typeof 123'number'
typeof "abc"'string'
typeof function() {}'function'
typeof {}'object'
typeof []'object'

5、$.isPlainObject()

判断指定参数是否是一个纯粹的对象(所谓"纯粹的对象",就是该对象是通过"{}"或"new Object"创建的。)

$.isPlainObject(obj)



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2150
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

评论列表