Check if a value is an object in JavaScript

后端 未结 30 3281
臣服心动
臣服心动 2020-11-22 05:06

How do you check if a value is an object in JavaScript?

相关标签:
30条回答
  • 2020-11-22 05:59

    Try this

    if (objectName instanceof Object == false) {
      alert('Not an object');
    }
    else {
      alert('An object');
    }
    
    0 讨论(0)
  • 2020-11-22 06:01

    Oh My God! I think this could be more shorter than ever, let see this:

    Short and Final code

    function isObject(obj)
    {
        return obj != null && obj.constructor.name === "Object"
    }
    
    console.log(isObject({})) // returns true
    console.log(isObject([])) // returns false
    console.log(isObject(null)) // returns false

    Explained

    Return Types

    typeof JavaScript objects (including null) returns "object"

    console.log(typeof null, typeof [], typeof {})

    Checking on Their constructors

    Checking on their constructor property returns function with their names.

    console.log(({}).constructor) // returns a function with name "Object"
    console.log(([]).constructor) // returns a function with name "Array"
    console.log((null).constructor) //throws an error because null does not actually have a property

    Introducing Function.name

    Function.name returns a readonly name of a function or "anonymous" for closures.

    console.log(({}).constructor.name) // returns "Object"
    console.log(([]).constructor.name) // returns "Array"
    console.log((null).constructor.name) //throws an error because null does not actually have a property

    Note: As of 2018, Function.name might not work in IE https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Browser_compatibility

    0 讨论(0)
  • 2020-11-22 06:01
    var a = [1]
    typeof a //"object"
    a instanceof Object //true
    a instanceof Array //true
    
    var b ={a: 1}
    b instanceof Object //true
    b instanceof Array //false
    
    var c = null
    c instanceof Object //false
    c instanceof Array //false
    

    I was asked to provide more details. Most clean and understandable way of checking if our variable is an object is typeof myVar. It returns a string with a type (e.g. "object", "undefined").

    Unfortunately either Array and null also have a type object. To take only real objects there is a need to check inheritance chain using instanceof operator. It will eliminate null, but Array has Object in inheritance chain.

    So the solution is:

    if (myVar instanceof Object && !(myVar instanceof Array)) {
      // code for objects
    }
    
    0 讨论(0)
  • 2020-11-22 06:01

    If you would like to check if the prototype for an object solely comes from Object. Filters out String, Number, Array, Arguments, etc.

    function isObject (n) {
      return Object.prototype.toString.call(n) === '[object Object]';
    }
    

    Or as a single-expression arrow function (ES6+)

    const isObject = n => Object.prototype.toString.call(n) === '[object Object]'
    
    0 讨论(0)
  • 2020-11-22 06:02

    Little late... for "plain objects" (i mean, like {'x': 5, 'y': 7}) i have this little snippet:

    function isPlainObject(o) {
       return (o === null || Array.isArray(o) || typeof o == 'function' || o.constructor === Date ) ?
               false
              :(typeof o == 'object');
    }
    

    It generates the next output:

    console.debug(isPlainObject(isPlainObject)); //function, false
    console.debug(isPlainObject({'x': 6, 'y': 16})); //literal object, true
    console.debug(isPlainObject(5)); //number, false
    console.debug(isPlainObject(undefined)); //undefined, false
    console.debug(isPlainObject(null)); //null, false
    console.debug(isPlainObject('a')); //string, false
    console.debug(isPlainObject([])); //array?, false
    console.debug(isPlainObject(true)); //bool, false
    console.debug(isPlainObject(false)); //bool, false
    

    It always works for me. If will return "true" only if the type of "o" is "object", but no null, or array, or function. :)

    0 讨论(0)
  • 2020-11-22 06:03

    lodash has isPlainObject, which might be what many who come to this page are looking for. It returns false when give a function or array.

    0 讨论(0)
提交回复
热议问题