# .= Operator

**URL:** <https://es.discourse.group/t/operator/586>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [December 17, 2020, 12:05am UTC](https://es.discourse.group/t/operator/586 "2020-12-17T00:05:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bagel03](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bagel03/32/1331_2.png) [@Bagel03](https://es.discourse.group/u/Bagel03)\
**Post date:** [December 17, 2020, 12:05am UTC](https://es.discourse.group/t/operator/586/1 "2020-12-17T00:05:18Z")

</div>

I have had I really simple idea for a while, and that is the `.=` operator. It would be simmilar to the `+=, -=, /=, *=` operators, except it is based on the supplied object. I am bad at explaining, so here is what I mean:

Right now I have to do this

```javascript
this.objects.player.position.vector = this.objects.player.position.vector.add(vel);

```

Now I could create a variable,

```javascript
const player = this.objects.player.position.vector;
player = player.add(vel);

```

But, this falls apart for types that are copied and not referenced.

What I propose is this:

```javascript
this.objects.player.position.vector .= add(vel);

```

This expands to the first code block:

```javascript
this.objects.player.position.vector = this.objects.player.position.vector.add(vel);

```

The `.=` operator takes whatever value is on the left, and looks for whatever is on the right in the prototype or in the object itself and applies that to the object on the left, then sets the object on the left to the final result. This is useful for things like `array.map` or functions that return a new object instead of modifying the original one.

---

<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:** [December 17, 2020, 12:32am UTC](https://es.discourse.group/t/operator/586/2 "2020-12-17T00:32:31Z")

</div>

How often do you find that fluent interfaces return a new value, as opposed to running side-effects or mutating the receiver?

For arrays, there's map and friends of course, but there's also `.reverse()`, `.sort()`, `.fill()`, `.splice()`, etc that mutate the original (and only some of these return `this`).

---

<div class="post-metadata">

**Author:** ![Bagel03](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bagel03/32/1331_2.png) [@Bagel03](https://es.discourse.group/u/Bagel03)\
**Post date:** [December 17, 2020, 1:05am UTC](https://es.discourse.group/t/operator/586/3 "2020-12-17T01:05:56Z")

</div>

Well I mean its not limited to arrays, it could be custom objects or classes, or another one that comes to mind is string.toLower/UpperCase(). It would really just be syntactic sugar for methods like that.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [December 17, 2020, 3:32pm UTC](https://es.discourse.group/t/operator/586/4 "2020-12-17T15:32:29Z")

</div>

Have a look at [Why isn't there a `.=` operator?](https://cs.stackexchange.com/questions/28874/why-isnt-there-a-operator-or-etc) for some arguments against doing this.
