npm.io
3.1.0 • Published 4 years ago

has-own-prop

Licence
MIT
Version
3.1.0
Deps
0
Size
3 kB
Vulns
0
Weekly
0
Stars
29
Native alternativeObject.prototype.hasOwnProperty.call(obj, prop) (or in later versions of node, "Object.hasOwn(obj, prop)")Use native JavaScript (Node 0.10.0+) Documentation

has-own-prop

A safer .hasOwnProperty()

Shortcut for Object.prototype.hasOwnProperty.call(object, property). Also comes with a type guard for TypeScript users.

You shouldn't use .hasOwnProperty() as it won't exist on objects created with Object.create(null) or it can have been overridden.

Install

npm install has-own-prop

Usage

import hasOwnProperty from 'has-own-prop';

const object = Object.create(null);
object.unicorn = true;

object.hasOwnProperty('unicorn');
//=> 'TypeError: undefined is not a function'

hasOwnProperty(object, 'unicorn');
//=> true

Keywords