Syntax to get same object that method was called on (Easy method chaining)
Caitlin Potter
caitpotter88 at gmail.com
Mon Oct 26 05:04:17 UTC 2015
I think, if you were going to have this, it would be best if bound `this` were ignored, and if it were a syntax error for non-property references.
Reason being, you'd actually be able to tell what was going on, mostly, by reading the source code. Otherwise it's really hard to tell what's going on.
A more interesting question could be super properties, since the appropriate receiver isn't the same as the object the method is looked up on
```js
super
.foo()
.bar(); // overridden bar or superclass bar?
```
Dart has something like this, do they have weird rules about `this` to work around too?
> On Oct 26, 2015, at 12:46 AM, Edwin Reynoso <eorroe at gmail.com> wrote:
>
> `foo()#` this one it depends, how `foo` is defined
>
> if `foo` is defined in the `window` scope, it'll return the `window`:
>
> if not and just like this in another scope, it should most likely throw:
>
> ```
> (function() {
> function foo() {};
> foo()# // throws
> })();
> ```
>
> `foo.bar.call(baz)#` would return `baz` because
>
> > (basically the `this` value of the method call)
>
> `baz.quux = foo.bind(bar)`, that's something that I'm not sure what's best, so not sure, but because of what I said above I think the right answer would be to return `bar` (the `this` value)
>
> `[].length#` will return the array
>
> `{ foo: 3 }.foo#` same thing as above, it'll return the object
>
>> On Mon, Oct 26, 2015 at 12:34 AM, Jordan Harband <ljharb at gmail.com> wrote:
>> 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
>
> _______________________________________________
> 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/20151026/bc68967b/attachment-0001.html>
More information about the es-discuss
mailing list