# Object.prototype.let

**URL:** <https://es.discourse.group/t/object-prototype-let/614>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [January 11, 2021, 9:59pm UTC](https://es.discourse.group/t/object-prototype-let/614 "2021-01-11T21:59:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [January 11, 2021, 9:59pm UTC](https://es.discourse.group/t/object-prototype-let/614/1 "2021-01-11T21:59:44Z")

</div>

## Idea

Instead of…

```javascript
const phoneNumber =
  data?.customer?.phoneNumber === undefined
    ? undefined
    : formatPhoneNumber(phoneNumber);

```

I want…

```javascript
const phoneNumber = data?.customer?.phoneNumber?.let(formatPhoneNumber);

```

or…

```javascript
const phoneNumber = data?.customer?.phoneNumber?.let((phoneNumber) =>
  formatPhoneNumber(phoneNumber)
);

```

## Possible Polyfill

```javascript
Object.prototype.let = function (callbackFn) {
  return callbackFn(this);
}

```

## Notes

The keyword could be something other than `let` but the idea would be the same: invoke a function passing the object itself as its single argument.

`apply` and `call` are already used in other contexts and should probably not be used unless it won't cause confusion.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [January 11, 2021, 10:22pm UTC](https://es.discourse.group/t/object-prototype-let/614/2 "2021-01-11T22:22:23Z")

</div>

Hi @mfulton26!

Adding new properties to `Object.prototype` is unlikely to happen as this could change the behaviour of existing apps.

You might be interested in this idea from @claudiameadows[Optional chaining support for the pipeline operator? · Issue #159 · tc39/proposal-pipeline-operator · GitHub](https://github.com/tc39/proposal-pipeline-operator/issues/159). This would let you write:

```javascript
const phoneNumber = data?.customer?.phoneNumber ?> formatPhoneNumber;

```

---

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [January 11, 2021, 10:31pm UTC](https://es.discourse.group/t/object-prototype-let/614/3 "2021-01-11T22:31:07Z")

</div>

I saw that issue too. Thank you for referencing it. It is similar. The pipeline operator itself is not yet supported by any browsers though.

It is true that this could change the behavior of existing apps but I believe TC39 must be able to move forward with iterating on built-in prototypes and leave existing apps to take care when modifying the prototype of objects that they do not own.

---

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [January 11, 2021, 10:33pm UTC](https://es.discourse.group/t/object-prototype-let/614/4 "2021-01-11T22:33:24Z")

</div>

FYI: there are several active proposals at the moment that add to existing prototypes (e.g. [tc39/proposal-relative-indexing-method: A TC39 proposal to add an .at() method to all the basic indexable classes (Array, String, TypedArray)](https://github.com/tc39/proposal-relative-indexing-method)).

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 11, 2021, 10:52pm UTC](https://es.discourse.group/t/object-prototype-let/614/5 "2021-01-11T22:52:22Z")

</div>

Adding to existing prototypes is one class of web compatibility risk; adding to Object.prototype is a much, much higher likelihood. Additionally, adding this wouldn't work for anything that inherits from `null`, like `Object.create(null)` or `import * as ns from 'path'`, so its usefulness would be limited.

---

<div class="post-metadata">

**Author:** ![mfulton26](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mfulton26/32/578_2.png) [@mfulton26](https://es.discourse.group/u/mfulton26)\
**Post date:** [January 11, 2021, 10:57pm UTC](https://es.discourse.group/t/object-prototype-let/614/6 "2021-01-11T22:57:46Z")

</div>

Interesting. So perhaps a language feature like an optional pipeline `?>` does make more sense too as it could work with an object with no prototype (like that from `Object.create(null)`).

---

<div class="post-metadata">

**Author:** ![septs](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/septs/32/395_2.png) [@septs](https://es.discourse.group/u/septs)\
**Post date:** [January 14, 2021, 6:18pm UTC](https://es.discourse.group/t/object-prototype-let/614/7 "2021-01-14T18:18:39Z")

</div>

> **[hax/proposal-extensions](https://github.com/hax/proposal-extensions)**
>
> Extensions proposal for ECMAScript. Contribute to hax/proposal-extensions development by creating an account on GitHub.
