Skip to content

Latest commit

 

History

History
53 lines (33 loc) · 1.31 KB

prefer-object-has-own.md

File metadata and controls

53 lines (33 loc) · 1.31 KB

Prefer Object.hasOwn() over Object.prototype.hasOwnProperty.call() (prefer-object-has-own)

It is very common to write code like:

if (Object.prototype.hasOwnProperty.call(object, "foo")) {
  console.log("has property foo");
}

This is a common practice because methods on Object.prototype can sometimes be unavailable or redefined (see the no-prototype-builtins rule).

Introduced in ES2022, Object.hasOwn() is a shorter alternative to Object.prototype.hasOwnProperty.call():

if (Object.hasOwn(object, "foo")) {
  console.log("has property foo")
}

Rule Details

Examples of incorrect code for this rule:

/*eslint prefer-object-has-own: "error"*/

Object.prototype.hasOwnProperty.call(obj, "a");

Object.hasOwnProperty.call(obj, "a");

({}).hasOwnProperty.call(obj, "a");

const hasProperty = Object.prototype.hasOwnProperty.call(object, property);

Examples of correct code for this rule:

/*eslint prefer-object-has-own: "error"*/

Object.hasOwn(obj, "a");

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

When Not To Use It

This rule should not be used unless ES2022 is supported in your codebase.

Further Reading