Check if a value is an object in JavaScript

你如何检查一个值是否是JavaScript中的对象?


Try using typeof(var) and/or var instanceof something .

EDIT: This answer gives an idea of how to examine variable's properties, but it is not a bulletproof recipe (after all there's no recipe at all!) for checking whether it's an object, far from it. Since people tend to look for something to copy from here without doing any research, I'd highly recommend that they turn to the other, most upvoted (and correct!) answer.


If typeof yourVariable === 'object' , it's an object or null. If you want to exclude null, just make it yourVariable !== null && typeof yourVariable === 'object' .


Let's define "object" in Javascript . According to the MDN docs, every value is either an object or a primitive:

primitive, primitive value

A data that is not an object and does not have any methods. JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined.

What's a primitive?

  • 3
  • 'abc'
  • true
  • null
  • undefined
  • What's an object (ie not a primitive)?

  • Object.prototype
  • everything descended from Object.prototype
  • Function.prototype
  • Object
  • Function
  • function C(){} -- user-defined functions
  • C.prototype -- the prototype property of a user-defined function: this is not C s prototype
  • new C() -- "new"-ing a user-defined function
  • Math
  • Array.prototype
  • arrays
  • {"a": 1, "b": 2} -- objects created using literal notation
  • new Number(3) -- wrappers around primitives
  • ... many other things ...
  • Object.create(null)
  • everything descended from an Object.create(null)
  • How to check whether a value is an object

    instanceof by itself won't work, because it misses two cases:

    // oops:  isObject(Object.prototype) -> false
    // oops:  isObject(Object.create(null)) -> false
    function isObject(val) {
        return val instanceof Object; 
    }
    

    typeof x === 'object' won't work, because of false positives ( null ) and false negatives (functions):

    // oops: isObject(Object) -> false
    function isObject(val) {
        return (typeof val === 'object');
    }
    

    Object.prototype.toString.call won't work, because of false positives for all of the primitives:

    > Object.prototype.toString.call(3)
    "[object Number]"
    
    > Object.prototype.toString.call(new Number(3))
    "[object Number]"
    

    So I use:

    function isObject(val) {
        if (val === null) { return false;}
        return ( (typeof val === 'function') || (typeof val === 'object') );
    }
    

    @Daan's answer also seems to work:

    function isObject(obj) {
      return obj === Object(obj);
    }
    

    because, according to the MDN docs:

    The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a type that corresponds to the given value. If the value is an object already, it will return the value.


    A third way that seems to work (not sure if it's 100%) is to use Object.getPrototypeOf which throws an exception if its argument isn't an object:

    // these 5 examples throw exceptions
    Object.getPrototypeOf(null)
    Object.getPrototypeOf(undefined)
    Object.getPrototypeOf(3)
    Object.getPrototypeOf('abc')
    Object.getPrototypeOf(true)
    
    // these 5 examples don't throw exceptions
    Object.getPrototypeOf(Object)
    Object.getPrototypeOf(Object.prototype)
    Object.getPrototypeOf(Object.create(null))
    Object.getPrototypeOf([])
    Object.getPrototypeOf({})
    
    链接地址: http://www.djcxy.com/p/5428.html

    上一篇: 如何测试空的JavaScript对象?

    下一篇: 检查一个值是否是JavaScript中的一个对象