Proposal: Symbol.inObject well-known symbol to customize the "in" operator's behavior

Tom Barrasso tom at barrasso.me
Thu May 9 18:52:02 UTC 2019


Like Symbol.hasInstance but for the "in" operator.
This symbol would work for both native and user-defined objects.

**Example implementation** prototyping native object:

```js
String.prototype[Symbol.inObject] =
  function(searchString) {
    return this.includes(searchString)
}
```


**Example implementation* *for user-defined object:

```js
function range(min, max) => ({
    [Symbol.inObject]: (prop) => {
        return (prop >= min && prop <= max)
    }
})
```


**Example usage**:

```js
("foo" in "food")    // true
(14 in range(1, 25)) // true
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190509/ab304f1e/attachment.html>


More information about the es-discuss mailing list