Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 1.18 KB

prefer-object-has-own.md

File metadata and controls

58 lines (41 loc) · 1.18 KB

Prefer Object.hasOwn(…) over Object.prototype.hasOwnProperty.call(…)

🔧 This rule is auto-fixable.

Object.hasOwn(…) is more accessible than Object.prototype.hasOwnProperty.call(…).

Fail

const hasProperty = Object.prototype.hasOwnProperty.call(object, property);
const hasProperty = {}.hasOwnProperty.call(object, property);
const hasProperty = lodash.has(object, property);

Pass

const hasProperty = Object.hasOwn(object, property);

Options

Type: object

functions

Type: string[]

You can also check custom functions that indicating the object has the specified property as its own property.

_.has(), lodash.has(), and underscore.has() are always checked.

Example:

{
	'unicorn/prefer-object-has-own': [
		'error',
		{
			functions: [
				'has',
				'utils.has',
			]
		}
	]
}
// eslint unicorn/prefer-object-has-own: ["error", {"functions": ["utils.has"]}]
const hasProperty = utils.has(object, property); // Fails