Syntax to get same object that method was called on (Easy method chaining)

Edwin Reynoso eorroe at gmail.com
Mon Oct 26 04:25:26 UTC 2015


Could we get a way to basically to get the object back from after a method
was called, so that instead of this:

```JS
let obj = {
 doSomething() {
   // some side effect
   return 5;
 },
 doSomething2() {
   // some other side effect
   return {x: 5};
 }
}

obj.doSomething();
obj.doSomething2();
```

We could do this:
```
obj.doSomething()#doSomething2();
```

Where `#` gets the object that the method was called on (basically the
`this` value of the method call)

Why?

There are lots of methods that don't return anything (they return
`undefined` by default) and instead of retyping the same object we could
have `#` give us the object back. There's also methods that do return
something but I may not want that value:

```
let arr = [1,2,3];
arr.push(4).forEach(function() {...}); // throws because the push method
returns the length of the array
```
With `#` I could do:

```
arr.push(4)#forEach(function() {...});
```

Ayy even `forEach` itself doesn't return, there's a [discussion](
https://esdiscuss.org/topic/return-value-of-foreach) on changing that
instead of breaking APIs which we can't we can have this:

```
arr.push(4)#forEach(function() {...});
```

which won't require any API changes, and could be used on any function.

Now I have no idea about implementation details, so not sure if this is
possible

thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151026/8bf67d7c/attachment.html>


More information about the es-discuss mailing list