File: breaking_changes_super_this.md

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,360 kB
  • sloc: makefile: 20; xml: 9; sh: 6; javascript: 5
file content (20 lines) | stat: -rw-r--r-- 595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
### `super` and `this`

In the constructor of a derived class (a class that `extends` another class), `this` cannot be used before calling `super`:

```coffee
class B extends A
  constructor: -> this  # Throws a compiler error
```
This also means you cannot pass a reference to `this` as an argument to `super` in the constructor of a derived class:

```coffee
class B extends A
  constructor: (@arg) ->
    super @arg  # Throws a compiler error
```
This is a limitation of ES2015 classes. As a workaround, assign to `this` after the `super` call:

```
codeFor('breaking_change_super_this')
```