Function polyObjHasOwn

  • The polyObjHasOwn() method is a polyfill for objHasOwn when the native Object.hasOwnreturns is not supported, it returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it). If the property is inherited, or does not exist, the method returns false.

    The objHasOwn() method returns true if the specified property is a direct property of the object — even if the property value is null or undefined. The method returns false if the property is inherited, or has not been declared at all. Unlike the in operator, this method does not check for the specified property in the object's prototype chain.

    It is recommended over objHasOwnProperty() because it works for objects created using objCreate(null) and with objects that have overridden the inherited hasOwnProperty() method. While it is possible to workaround these problems by calling Object.prototype.hasOwnProperty() on an external object, Object.hasOwn() is more intuitive.

    Type Parameters

    • T = any

    Parameters

    • obj: T

      The object being evaluated

    • prop: PropertyKey

      The String or Symbol of the property to test

    Returns boolean

    true if the object has the specified property as own property; otherwise false

    0.4.3

    let example = {};
    polyObjHasOwn(example, 'prop'); // false

    example.prop = 'exists';
    polyObjHasOwn(example, 'prop'); // true - 'prop' has been defined

    example.prop = null;
    polyObjHasOwn(example, 'prop'); // true - own property exists with value of null

    example.prop = undefined;
    polyObjHasOwn(example, 'prop'); // true - own property exists with value of undefined