Coder Social home page Coder Social logo

nicolo-ribaudo / proposal-accessible-object-hasownproperty Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tc39/proposal-accessible-object-hasownproperty

0.0 1.0 0.0 119 KB

Object.hasOwn() proposal for ECMAScript

Home Page: https://tc39-transfer.github.io/proposal-accessible-object-hasownproperty/

License: MIT License

HTML 99.48% JavaScript 0.52%

proposal-accessible-object-hasownproperty's Introduction

Object.has()

Proposal for an Object.has() method to make Object.prototype.hasOwnProperty() more accessible.

Status

This proposal is currently at Stage 2

Authors:

Slides:

Motivation

Today, it is very common (especially in library code) to write code like:

let hasOwnProperty = Object.prototype.hasOwnProperty

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

This proposal simplifies that code to:

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

There are a number of existing libraries which make this more convenient:

This is a common practices because methods on Object.prototype can sometimes be unavailable or redefined.

Object.create(null)

Object.create(null) will create an object that does not inherit from Object.prototype, making those methods inaccessible.

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

Redefining hasOwnProperty

If you do not directly own every property defined of an object, you can't be 100% certain that calling .hasOwnProperty() is calling the built-in method:

let object = {
  hasOwnProperty() {
    throw new Error("gotcha!")
  }
}

object.hasOwnProperty("foo")
// Uncaught Error: gotcha!

ESLint no-prototype-builtins

ESLint has a built-in rule for banning use of prototype builtins like hasOwnProperty.

From the ESLint documentation for no-prototype-builtins:


Examples of incorrect code for this rule:

/*eslint no-prototype-builtins: "error"*/
var hasBarProperty = foo.hasOwnProperty("bar");
...

Examples of correct code for this rule:

/*eslint no-prototype-builtins: "error"*/
var hasBarProperty = Object.prototype.hasOwnProperty.call(foo, "bar");
...

MDN hasOwnProperty() advice

The MDN documentation for Object.prototype.hasOwnProperty includes advice not to use it off of the prototype chain directly:

JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results [...]

Proposal

This proposal adds a Object.has(object, property) method with the same behavior as calling hasOwnProperty.call(object, property)

let object = { foo: false }
Object.has(object, "foo") // true

let object2 = Object.create({ foo: true })
Object.has(object2, "foo") // false

let object3 = Object.create(null)
Object.has(object3, "foo") // false

Implementations

There are currently no native implementations of Object.has in JavaScript engines.

A polyfill of Object.has() is available in polyfill.js.

Q&A

Why not Object.hasOwnProperty(object, property)?

Object.hasOwnProperty(property) already exists today because Object itself inherits from Object.prototype so defining a new method with a different signature would be a breaking change.

Why the name has?

has is a popular name for this function in user-land library code (See Related). It also fills in a hole in the common operations between Object, Map, and Set.

Object.keys(o)
Object.values(o)
Object.entries(o)
Object.has(o, p) // New!

Map.prototype.keys()
Map.prototype.values()
Map.prototype.entries()
Map.prototype.has(p)

Set.prototype.keys()
Set.prototype.values()
Set.prototype.entries()
Set.prototype.has(p)

An alternative option would be Object.hasOwn() has been discussed in order to distinguish this method from Reflect.has() but there seems to be a stronger argument that this method should be named after the matching methods on Map, Set, FormData, URLSearchParams, and more.

Why not use Map for dictionaries instead of objects?

Excerpt from https://v8.dev/features/object-fromentries#objects-vs.-maps

JavaScript also supports Maps, which are often a more suitable data structure than regular objects. So in code that you have full control over, you might be using maps instead of objects. However, as a developer, you do not always get to choose the representation. Sometimes the data you’re operating on comes from an external API or from some library function that gives you an object instead of a map.

Why not place this method on Reflect?

The purpose of Reflect is to contain, 1:1, a method for each Proxy trap. There is already a method on Proxy that traps hasOwnProperty (getOwnPropertyDescriptor) so it doesn't make sense to add an additional trap, therefore it doesn't make sense to place this method on Reflect.

Related

proposal-accessible-object-hasownproperty's People

Contributors

jamiebuilds avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.