Promise() vs. new Promise()

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Aug 21 10:25:11 PDT 2014


I guess you haven't read the following code that does that in ES5 fashion
:-)

mixing up syntax

```
Object.defineProperty(
  Function.prototype,
  'new',
  {
    configurable: true,
    get: function () {
      return Object.defineProperty(
        this,
        'new',
        {value: (...args)=>new this(...args)}
      ).new;
    }
  }
);
```

But since most of the world of JS is scared to death since ES3 era to
extend natives prototypes, wouldn't be nice to spec it as such natively in
ES6 or ES7 ?

Regards





On Thu, Aug 21, 2014 at 5:28 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

>
> On Aug 20, 2014, at 7:30 PM, Andrea Giammarchi wrote:
>
> > I like the `Function.prototype.new` hint but unless it's an implicitly
> > self bound method you still cannot easily pass it around as callback
> > for a map without binding it all over.
> >
> > `arr.map(MyClass.new)` without needing to explicitly bind it each time
> > is needed/used would be an exception, but probably a good one.
>
> true, but you can write:
>
> arr.map((...a)=>new Map(...a));
>
> or define:
>
> Object.assign(Function.prototype, {
>    get asFactory() {return (...args)=>new this(...args)}
> }
>
> so you can say:
>
> arr.map(MyClass.asFactory)
>
> Allen
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140821/2964ddf6/attachment.html>


More information about the es-discuss mailing list