Proposal for faster this assignments in constructor functions

Jordan Harband ljharb at gmail.com
Thu Dec 6 19:24:58 UTC 2018


Your imagined syntax would suggest to me that the constructor had 5
arguments, not 3 - it seems strange to me to have "x" and "a" be implicitly
two ways to refer to the same thing.

On Thu, Dec 6, 2018 at 8:56 AM dante federici <c.dante.federici at gmail.com>
wrote:

> Any reason we can't consider a scala-like constructor?
>
> ```js
> class Point(x, y) {
>   toString() {
>     return `(${this.x},${this.y})`
>   }
> }
>
> console.log(`??? ${new Point(1, 2)}`); // ??? 1,2
> ````
>
> Doesn't conflict with existing syntax, it's clear, and you can still
> shorthand most data-classes or structs to just:
> ```
> class MyData(some, props, here) {}
> ```
>
> I imagine this as the syntax:
> ```
> class Point(x, y) { // implicits x, y as first two args of constru
>   constructor(a, b, c) {
>     this.z = a + b + c;
>     console.log(this.x, this.y, this.c); // values of: a, b, c
>     console.log(this.x === a, this.y === b); // true, true
>   }
> }
> ```
>
> And de-sugared:
> ```
> class Point {
>   constructor(a, b, c) {
>     this.x = a;
>     this.y = b;
>     // rest of body
>     this.z = a + b + c;
>   }
> }
> ```
>
> _______________________________________________
> 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/20181206/8a0d78c8/attachment.html>


More information about the es-discuss mailing list