babel-plugin-transform-new-target
This plugins allows babel to transform new.target
meta property into a
(correct in most cases) this.constructor
expression.
Example
{ console;} ; // => undefined; // => Foo
{ console; } ; // => Foo; // => Bar
Caveats
This plugin relies on this.constructor
, which means super
must
already have been called when using untransformed classes.
{} { // This will be a problem if classes aren't transformed to ES5 newtarget; super; }
Additionally, this plugin cannot transform all Reflect.construct
cases
when using newTarget
with ES5 function classes (transformed ES6 classes).
{ console;} // Bar extends Foo in ES5 { Foo;}Barprototype = Object;Barprototypeconstructor = Bar; // Baz does not extend Foo {} Reflect; // => Foo (correct)Reflect; // => Bar (correct) Reflect; // => Bar (incorrect, though this is how ES5 // inheritience is commonly implemented.)Reflect; // => undefined (incorrect)
Installation
npm install --save-dev babel-plugin-transform-new-target
Usage
.babelrc
(Recommended)
Via .babelrc
Via CLI
babel --plugins transform-new-target script.js
Via Node API
;