
先声明一个对象
var obj = {};判断是否为对象的方法:
1、toString(推荐)
Object.prototype.toString.call(obj) === '[Object Object]'
2、constructor属性
constructor 属性返回对创建此对象的 Boolean 函数的引用。
obj.constructor === Object
3、instanceof运算符
obj instanceof Object
注:需要注意的是由于数组也是对象,因此用 arr instanceof Object 也为true。
4、typeof运算符
typeof运算符返回一个字符串,表示未经计算的操作数的类型。
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'5、$.isPlainObject()
该方法判断指定参数是否是一个纯粹的对象(所谓"纯粹的对象",就是该对象是通过"{}"或"new Object"创建的。)
$.isPlainObject(obj)
以上就是javascript如何判断是否为对象?的详细内容,更多请关注易知道|edz.cc其它相关文章!













