const {resolve} = Promise; // fails

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Jul 19 16:27:00 UTC 2018


> Per the KISS principle, let’s avoid to be clever.

I think my next code example is less clever, but the only reason I've
written hacks or code was not to be advocated or adopted, just to explain
what could happen internally.

TL;DR why aren't public static methods that need context **all** lazily
defined as bound (once) on demand? That would make every single public
static method consistent, accordingly with the Class you extracted them
from, right? That's not clever, that's usually developers expectations
because historically all public static methods don't need the Class to work.

On Thu, Jul 19, 2018 at 5:23 PM Claude Pache <claude.pache at gmail.com> wrote:

>
>
> > Le 19 juil. 2018 à 16:32, Andrea Giammarchi <andrea.giammarchi at gmail.com>
> a écrit :
> >
> > I know it's about subclassing, which is why I've asked why, once there's
> no context, the default/base one is not considered, but since everyone came
> back with the subclassing issue, which is actually what I've said myself on
> twitter about the current state, how about changing all public static
> methods that need it, to be getters ?
> >
> > ```js
> > class Promise {
> >   #resolve(...args) {
> >     return this.nativeImplementation(...args);
> >   }
> >   get resolve() {
> >     return #resolve.bind(this);
> >   }
> > }
> > ```
> >
> > we could argue `Promise.resolve === Promise.resolve` should be
> preserved, as behavior, so that we need a lazy defined getter ... **but**
> why not making public static restructuring from known constructors work
> regardless, under all circumstances ?
> >
>
> Nice hack... But it imposes all subclasses of `Promise` that override the
> `resolve` method to use a similar trick, because the following will break:
>
> ```js
> class MyPromise extends Promise {
>     static resolve() {
>         // do fancy stuff
>         return super.resolve()
>     }
> }
>
> const {resolve} = MyPromise
>
> resolve() // TypeError: undefined is not an object
> ```
>
> Per the KISS principle, let’s avoid to be clever.
>
> —Claude
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180719/3a8d8f0d/attachment.html>


More information about the es-discuss mailing list