Skip to content

Function: hasOwnProperty()

ts
function hasOwnProperty(obj, key): boolean;

Safely checks if an object has a property defined on itself (not inherited). 安全地检查对象是否具有自身定义的属性(非继承)。

Parameters

ParameterTypeDescription
objunknownThe object to check / 要检查的对象
keyPropertyKeyThe property key to check for / 要检查的属性键

Returns

boolean

True if the object has the own property / 如果对象具有自身属性则返回true

Example

typescript
const obj = { foo: 42, bar: 'hello' }
const arr = [1, 2, 3]

hasOwnProperty(obj, 'foo') // => true
hasOwnProperty(obj, 'toString') // => false (inherited from Object.prototype)
hasOwnProperty(obj, 'hasOwnProperty') // => false (inherited)

hasOwnProperty(arr, '0') // => true
hasOwnProperty(arr, 0) // => true (number key)
hasOwnProperty(arr, 'length') // => true
hasOwnProperty(arr, 'push') // => false (inherited from Array.prototype)

// Safe for objects without prototype
const nullObj = Object.create(null)
nullObj.prop = 'value'
hasOwnProperty(nullObj, 'prop') // => true

// Works with symbols
const sym = Symbol('test')
const objWithSymbol = { [sym]: 'value' }
hasOwnProperty(objWithSymbol, sym) // => true

Since

1.0.0

Released under the MIT License.