Syntax to get same object that method was called on (Easy method chaining)
Jordan Harband
ljharb at gmail.com
Mon Oct 26 04:34:32 UTC 2015
This immediately raises some questions for me:
- what would `foo()#` return? (a bare function call - the question applies
to strict mode, and sloppy mode)
- what would `foo.bar.call(baz)#` return? (foo? or baz?)
- what would `baz.quux = foo.bind(bar); baz.quux()#` return? (baz? or bar?)
- what would `[].length#` return? (an accessor property) (throw? the
array? undefined?)
- what would `{ foo: 3 }.foo#` return? (a data property) (throw? the
object? undefined?)
On Sun, Oct 25, 2015 at 9:25 PM, Edwin Reynoso <eorroe at gmail.com> wrote:
> 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?
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151025/66337d40/attachment-0001.html>
More information about the es-discuss
mailing list