Field initializers

just nobody kingdaro at gmail.com
Sat Mar 18 18:00:53 UTC 2017


Inherited methods would inherit the same behavior, since this is just
syntactic sugar after all.
```js
class Point {
  setPosition(this.x, this.y) {}

  // same as
  // setPosition(x, y) { this.x = x; this.y = y }
}

class Rectangle extends Point {
  setSize(this.width, this.height) {}

  // same as
  // setSize(width, height) { this.width = width; this.height = height }
}

let rect = new Rectangle()
rect.setPosition(0, 0)
rect.setSize(50, 50)
rect // { x: 0, y: 0, width: 50, height: 50 }
```

On Sat, Mar 18, 2017 at 7:12 AM Michael J. Ryan <tracker1 at gmail.com> wrote:

> Interesting concept... What about inheritance?
>
> --
> Michael J. Ryan - tracker1 at gmail.com - http://tracker1.info
>
> Please excuse grammar errors and typos, as this message was sent from my
> phone.
>
> On Mar 18, 2017 12:03 AM, "just nobody" <kingdaro at gmail.com> wrote:
>
> Would it be possible to have syntax like this? It's a feature supported by
> other JS variants in some way (CoffeeScript, TS) that feels missing from
> the spec. It's mainly useful as a convenient, terse way of setting
> properties of an object on initialization.
>
> ```js
> class Rectangle {
>   constructor (this.x, this.y, this.width, this.height) {}
>   setPosition(this.x, this.y) {}
>   setSize(this.width, this.height) {}
> }
>
> // equivalent to
> class Rectangle {
>   constructor (x, y, width, height) {
>     this.x = x
>     this.y = y
>     this.width = width
>     this.height = height
>   }
>
>   // ...
> }
>
> // for regular constructor functions as well
> function Rectangle (this.x, this.y, this.width, this.height) {}
> ```
>
> Deconstructing and argument defaults should all work similarly
> ```js
> function Point({ x: this.x, y: this.y }) {}
> function Point(this.x = 0, this.y = 0) {}
> function Point([this.x, this.y]) {}
> ```
>
>
> _______________________________________________
> 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/20170318/e866454f/attachment-0001.html>


More information about the es-discuss mailing list