1

The question is how to get access to class static properties from within a constructor before super method was called?

class A
{
    constructor(input) {
        console.log('A', typeof new.target);
    }
}

class B extends A
{
    static template = '';

    constructor() {
        console.log('B', typeof new.target);
        super();
    }
}

class C extends B
{
    static template = 'CCC';
}

new C();

For some reason I got:

B undefined
A undefined

instead of

B function
A function

I've already asked this question about a year ago. For now, the solution provided in it is no more workable.

You can try the code in babel console. The interesting part is that this code works fine without babel (e.g. in latest Chrome), and when es2015 checkbox is off.

Community
  • 1
  • 1
vbarbarosh
  • 3,502
  • 4
  • 33
  • 43
  • This only suggests that there is a Babel bug, not that `new.target` is not working like it did. You should report that `new.target` is not transpiled at all any more, and use an older Babel version for the time being. – Bergi Aug 25 '16 at 22:21
  • https://phabricator.babeljs.io/T1088 – Bergi Aug 26 '16 at 00:52

1 Answers1

1

This seems to be a bug in babel. I wrote a small plugin that transpiles new.target:

https://github.com/vbarbarosh/babel_plugin_transform_es2015_newtarget

$ npm install --save-dev git://github.com/vbarbarosh/babel_plugin_transform_es2015_newtarget
$ cat .babelrc
...
    "plugins": ["transform-es2015-newtarget"],
...
vbarbarosh
  • 3,502
  • 4
  • 33
  • 43