Check if a value is an object in JavaScript

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

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

30条回答
  •  悲&欢浪女
    2020-11-22 05:51

    Ready to use functions for checking

    function isObject(o) {
      return null != o && 
        typeof o === 'object' && 
        Object.prototype.toString.call(o) === '[object Object]';
    }
    
    function isDerivedObject(o) {
      return !isObject(o) && 
        null != o && 
        (typeof o === 'object' || typeof o === 'function') &&
        /^\[object /.test(Object.prototype.toString.call(o));
    }
    
    // Loose equality operator (==) is intentionally used to check
    // for undefined too
    
    // Also note that, even null is an object, within isDerivedObject
    // function we skip that and always return false for null
    

    Explanation

    • In Javascript, null, Object, Array, Date and functions are all objects. Although, null is bit contrived. So, it's better to check for the null first, to detect it's not null.

    • Checking for typeof o === 'object' guarantees that o is an object. Without this check, Object.prototype.toString would be meaningless, since it would return object for everthing, even for undefined and null! For example: toString(undefined) returns [object Undefined]!

      After typeof o === 'object' check, toString.call(o) is a great method to check whether o is an object, a derived object like Array, Date or a function.

    • In isDerivedObject function, it checks for the o is a function. Because, function also an object, that's why it's there. If it didn't do that, function will return as false. Example: isDerivedObject(function() {}) would return false, however now it returns true.

    • One can always change the definition of what is an object. So, one can change these functions accordingly.


    Tests

    function isObject(o) {
      return null != o && 
        typeof o === 'object' && 
        Object.prototype.toString.call(o) === '[object Object]';
    }
    
    function isDerivedObject(o) {
      return !isObject(o) && 
        null != o && 
        (typeof o === 'object' || typeof o === 'function') &&
        /^\[object /.test(Object.prototype.toString.call(o));
    }
    
    // TESTS
    
    // is null an object?
    
    console.log(
      'is null an object?', isObject(null)
    );
    
    console.log(
      'is null a derived object?', isDerivedObject(null)
    );
    
    // is 1234 an object?
    
    console.log(
      'is 1234 an object?', isObject(1234)
    );
    
    console.log(
      'is 1234 a derived object?', isDerivedObject(1234)
    );
    
    // is new Number(1234) an object?
    
    console.log(
      'is new Number(1234) an object?', isObject(new Number(1234))
    );
    
    console.log(
      'is new Number(1234) a derived object?', isDerivedObject(1234)
    );
    
    // is function object an object?
    
    console.log(
      'is (new (function (){})) an object?', 
      isObject((new (function (){})))
    );
    
    console.log(
      'is (new (function (){})) a derived object?', 
      isObject((new (function (){})))
    );
    
    // is {} an object?
    
    console.log(
      'is {} an object?', isObject({})
    );
    
    console.log(
      'is {} a derived object?', isDerivedObject({})
    );
    
    // is Array an object?
    
    console.log(
      'is Array an object?',
      isObject([])
    )
    
    console.log(
      'is Array a derived object?',
      isDerivedObject([])
    )
    
    // is Date an object?
    
    console.log(
      'is Date an object?', isObject(new Date())
    );
    
    console.log(
      'is Date a derived object?', isDerivedObject(new Date())
    );
    
    // is function an object?
    
    console.log(
      'is function an object?', isObject(function(){})
    );
    
    console.log(
      'is function a derived object?', isDerivedObject(function(){})
    );

提交回复
热议问题