Object. HasOwn replace Object. The prototype. The hasOwnProperty

Released 25 June 2021 · marked as ECMAScript

Recommend the use of the Object. HasOwn () method, because it makes Object. The prototype. The hasOwnProperty () is more easy to use.

phase

The proposal is still in its third phase

Why does the Object.hasown proposal exist?

Currently, code like this is common:

const hasOwnProperty = Object.prototype.hasOwnProperty;



if (hasOwnProperty.call(object.'foo')) {

  // `object` has property `foo`.

}

Copy the code

Or some library makes use of Object. The prototype. HasOwnProperty is simpler: NPM: from the NPM: lodash. From the See Related

Using the new object. hasOwn function, we can abbreviate the above code as:

if (Object.hasOwn(object, 'foo')) {

  // `object` has property `foo`.

}

Copy the code

In everyday development, there are some common uses where methods on Object.Prototype may not be available or may be redefined.

Such as:

1. Object. The create (null) :

Object.create(null) creates an Object that does not inherit from Object.Prototype, making methods on Object.Prototype inaccessible.

Object.create(null).hasOwnProperty("foo")

// Uncaught TypeError: Object.create(...) .hasOwnProperty is not a function

Copy the code

2. Redefine hasOwnProperty:

If you reassign a built-in property of an object, then when you call a property such as.hasownProperty, you must not call the built-in property of the object

let object = {

  hasOwnProperty() {

    throw new Error("gotcha!")

  }

}



object.hasOwnProperty("foo")

// Uncaught Error: gotcha!

Copy the code

3. ESLint no-prototype-builtins

In ESLint’s built-in rule, direct use of object.prototypes built-in functions is prohibited

No-prototype-builtins in the ESLint documentation:

Incorrect examples of this rule:

/*eslint no-prototype-builtins: "error"*/

var hasBarProperty = foo.hasOwnProperty("bar");

.

Copy the code

A good example of this rule:

/*eslint no-prototype-builtins: "error"*/

var hasBarProperty = Object.prototype.hasOwnProperty.call(foo, "bar");

.

Copy the code

The proposal

This proposal adds an Object.hasown (Object, property) method that behaves the same as calling hasownProperty.Call (Object, property)

let object = { foofalse }

Object.hasOwn(object, "foo"// true



let object2 = Object.create({ footrue })

Object.hasOwn(object2, "foo"// false



let object3 = Object.create(null)

Object.hasOwn(object3, "foo"// false

Copy the code

Why not use Object.hasownProperty (Object, property)?

Object.hasownproperty (Property) already exists today, and since Object itself inherits from Object.prototype, defining a new method with a different name would be an obvious change.

Why hasOwn?

See Issue #3

Object.hasOwn is already available in V8 V9.3, with the harmony-object-has-own flag at the end of the execution, and will soon be available in Chrome.

reference

  • Accessible Object.prototype.hasOwnProperty()

Social information/Social Links

  • Making: @ huangyangquang
  • Wechat official account: Joshua, upperclassman in front