oldie but goldie ? ```js Object.defineProperty( Function.prototype, 'new', { configurable: true, value(...args) { return new this(...args); } } ); ```
On Sun, Nov 5, 2017 at 11:28 AM, Oriol _ <oriol-bugzi...@hotmail.com> wrote: > > Why can't `new` be optional? > > When you call a function, you are using the internal [[Call]] method. When > you use the `new` operator, it's the internal [[Construct]] method. > > They are different things, so IMO avoiding `new` when you are > instantiating is bad practice. > > But if you really want to avoid `new` when using ES6 `class` syntax, you > can use proxies, e.g. > > ```js > let MyClass = new Proxy(class MyClass { > constructor(arg) { this.arg = arg; } > }, { > apply: (target, thisArg, args) => Reflect.construct(target, args) > }); > MyClass(1); // { arg: 1 } > new MyClass(2); // { arg: 2 } > ``` > > --Oriol > > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss